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 
260 void avio_wl32(AVIOContext *s, unsigned int val)
261 {
262  avio_w8(s, val);
263  avio_w8(s, val >> 8);
264  avio_w8(s, val >> 16);
265  avio_w8(s, val >> 24);
266 }
267 
268 void avio_wb32(AVIOContext *s, unsigned int val)
269 {
270  avio_w8(s, val >> 24);
271  avio_w8(s, val >> 16);
272  avio_w8(s, val >> 8);
273  avio_w8(s, val);
274 }
275 
276 int avio_put_str(AVIOContext *s, const char *str)
277 {
278  int len = 1;
279  if (str) {
280  len += strlen(str);
281  avio_write(s, (const unsigned char *) str, len);
282  } else
283  avio_w8(s, 0);
284  return len;
285 }
286 
287 int avio_put_str16le(AVIOContext *s, const char *str)
288 {
289  const uint8_t *q = str;
290  int ret = 0;
291 
292  while (*q) {
293  uint32_t ch;
294  uint16_t tmp;
295 
296  GET_UTF8(ch, *q++, break;)
297  PUT_UTF16(ch, tmp, avio_wl16(s, tmp); ret += 2;)
298  }
299  avio_wl16(s, 0);
300  ret += 2;
301  return ret;
302 }
303 
304 int ff_get_v_length(uint64_t val)
305 {
306  int i = 1;
307 
308  while (val >>= 7)
309  i++;
310 
311  return i;
312 }
313 
314 void ff_put_v(AVIOContext *bc, uint64_t val)
315 {
316  int i = ff_get_v_length(val);
317 
318  while (--i > 0)
319  avio_w8(bc, 128 | (val >> (7 * i)));
320 
321  avio_w8(bc, val & 127);
322 }
323 
324 void avio_wl64(AVIOContext *s, uint64_t val)
325 {
326  avio_wl32(s, (uint32_t)(val & 0xffffffff));
327  avio_wl32(s, (uint32_t)(val >> 32));
328 }
329 
330 void avio_wb64(AVIOContext *s, uint64_t val)
331 {
332  avio_wb32(s, (uint32_t)(val >> 32));
333  avio_wb32(s, (uint32_t)(val & 0xffffffff));
334 }
335 
336 void avio_wl16(AVIOContext *s, unsigned int val)
337 {
338  avio_w8(s, val);
339  avio_w8(s, val >> 8);
340 }
341 
342 void avio_wb16(AVIOContext *s, unsigned int val)
343 {
344  avio_w8(s, val >> 8);
345  avio_w8(s, val);
346 }
347 
348 void avio_wl24(AVIOContext *s, unsigned int val)
349 {
350  avio_wl16(s, val & 0xffff);
351  avio_w8(s, val >> 16);
352 }
353 
354 void avio_wb24(AVIOContext *s, unsigned int val)
355 {
356  avio_wb16(s, val >> 8);
357  avio_w8(s, val);
358 }
359 
360 /* Input stream */
361 
362 static void fill_buffer(AVIOContext *s)
363 {
364  uint8_t *dst = !s->max_packet_size &&
365  s->buf_end - s->buffer < s->buffer_size ?
366  s->buf_end : s->buffer;
367  int len = s->buffer_size - (dst - s->buffer);
368  int max_buffer_size = s->max_packet_size ?
370 
371  /* can't fill the buffer without read_packet, just set EOF if appropriate */
372  if (!s->read_packet && s->buf_ptr >= s->buf_end)
373  s->eof_reached = 1;
374 
375  /* no need to do anything if EOF already reached */
376  if (s->eof_reached)
377  return;
378 
379  if (s->update_checksum && dst == s->buffer) {
380  if (s->buf_end > s->checksum_ptr)
382  s->buf_end - s->checksum_ptr);
383  s->checksum_ptr = s->buffer;
384  }
385 
386  /* make buffer smaller in case it ended up large after probing */
387  if (s->buffer_size > max_buffer_size) {
388  ffio_set_buf_size(s, max_buffer_size);
389 
390  s->checksum_ptr = dst = s->buffer;
391  len = s->buffer_size;
392  }
393 
394  if (s->read_packet)
395  len = s->read_packet(s->opaque, dst, len);
396  else
397  len = 0;
398  if (len <= 0) {
399  /* do not modify buffer if EOF reached so that a seek back can
400  be done without rereading data */
401  s->eof_reached = 1;
402  if (len < 0)
403  s->error = len;
404  } else {
405  s->pos += len;
406  s->buf_ptr = dst;
407  s->buf_end = dst + len;
408  }
409 }
410 
411 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
412  unsigned int len)
413 {
414  return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
415 }
416 
417 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
418  unsigned int len)
419 {
420  return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
421 }
422 
424 {
426  s->buf_ptr - s->checksum_ptr);
427  s->update_checksum = NULL;
428  return s->checksum;
429 }
430 
432  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
433  unsigned long checksum)
434 {
435  s->update_checksum = update_checksum;
436  if (s->update_checksum) {
437  s->checksum = checksum;
438  s->checksum_ptr = s->buf_ptr;
439  }
440 }
441 
442 /* XXX: put an inline version */
444 {
445  if (s->buf_ptr >= s->buf_end)
446  fill_buffer(s);
447  if (s->buf_ptr < s->buf_end)
448  return *s->buf_ptr++;
449  return 0;
450 }
451 
452 int avio_read(AVIOContext *s, unsigned char *buf, int size)
453 {
454  int len, size1;
455 
456  size1 = size;
457  while (size > 0) {
458  len = s->buf_end - s->buf_ptr;
459  if (len > size)
460  len = size;
461  if (len == 0 || s->write_flag) {
462  if(size > s->buffer_size && !s->update_checksum){
463  if(s->read_packet)
464  len = s->read_packet(s->opaque, buf, size);
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  break;
472  } else {
473  s->pos += len;
474  size -= len;
475  buf += len;
476  s->buf_ptr = s->buffer;
477  s->buf_end = s->buffer/* + len*/;
478  }
479  } else {
480  fill_buffer(s);
481  len = s->buf_end - s->buf_ptr;
482  if (len == 0)
483  break;
484  }
485  } else {
486  memcpy(buf, s->buf_ptr, len);
487  buf += len;
488  s->buf_ptr += len;
489  size -= len;
490  }
491  }
492  if (size1 == size) {
493  if (s->error) return s->error;
494  if (s->eof_reached) return AVERROR_EOF;
495  }
496  return size1 - size;
497 }
498 
499 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
500 {
501  if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
502  *data = s->buf_ptr;
503  s->buf_ptr += size;
504  return size;
505  } else {
506  *data = buf;
507  return avio_read(s, buf, size);
508  }
509 }
510 
511 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
512 {
513  int len;
514 
515  if (size < 0)
516  return -1;
517 
518  if (s->read_packet && s->write_flag) {
519  len = s->read_packet(s->opaque, buf, size);
520  if (len > 0)
521  s->pos += len;
522  return len;
523  }
524 
525  len = s->buf_end - s->buf_ptr;
526  if (len == 0) {
527  /* Reset the buf_end pointer to the start of the buffer, to make sure
528  * the fill_buffer call tries to read as much data as fits into the
529  * full buffer, instead of just what space is left after buf_end.
530  * This avoids returning partial packets at the end of the buffer,
531  * for packet based inputs.
532  */
533  s->buf_end = s->buf_ptr = s->buffer;
534  fill_buffer(s);
535  len = s->buf_end - s->buf_ptr;
536  }
537  if (len > size)
538  len = size;
539  memcpy(buf, s->buf_ptr, len);
540  s->buf_ptr += len;
541  if (!len) {
542  if (s->error) return s->error;
543  if (s->eof_reached) return AVERROR_EOF;
544  }
545  return len;
546 }
547 
548 unsigned int avio_rl16(AVIOContext *s)
549 {
550  unsigned int val;
551  val = avio_r8(s);
552  val |= avio_r8(s) << 8;
553  return val;
554 }
555 
556 unsigned int avio_rl24(AVIOContext *s)
557 {
558  unsigned int val;
559  val = avio_rl16(s);
560  val |= avio_r8(s) << 16;
561  return val;
562 }
563 
564 unsigned int avio_rl32(AVIOContext *s)
565 {
566  unsigned int val;
567  val = avio_rl16(s);
568  val |= avio_rl16(s) << 16;
569  return val;
570 }
571 
572 uint64_t avio_rl64(AVIOContext *s)
573 {
574  uint64_t val;
575  val = (uint64_t)avio_rl32(s);
576  val |= (uint64_t)avio_rl32(s) << 32;
577  return val;
578 }
579 
580 unsigned int avio_rb16(AVIOContext *s)
581 {
582  unsigned int val;
583  val = avio_r8(s) << 8;
584  val |= avio_r8(s);
585  return val;
586 }
587 
588 unsigned int avio_rb24(AVIOContext *s)
589 {
590  unsigned int val;
591  val = avio_rb16(s) << 8;
592  val |= avio_r8(s);
593  return val;
594 }
595 unsigned int avio_rb32(AVIOContext *s)
596 {
597  unsigned int val;
598  val = avio_rb16(s) << 16;
599  val |= avio_rb16(s);
600  return val;
601 }
602 
603 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
604 {
605  int i = 0;
606  char c;
607 
608  do {
609  c = avio_r8(s);
610  if (c && i < maxlen-1)
611  buf[i++] = c;
612  } while (c != '\n' && c);
613 
614  buf[i] = 0;
615  return i;
616 }
617 
618 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
619 {
620  int i;
621 
622  if (buflen <= 0)
623  return AVERROR(EINVAL);
624  // reserve 1 byte for terminating 0
625  buflen = FFMIN(buflen - 1, maxlen);
626  for (i = 0; i < buflen; i++)
627  if (!(buf[i] = avio_r8(s)))
628  return i + 1;
629  buf[i] = 0;
630  for (; i < maxlen; i++)
631  if (!avio_r8(s))
632  return i + 1;
633  return maxlen;
634 }
635 
636 #define GET_STR16(type, read) \
637  int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
638 {\
639  char* q = buf;\
640  int ret = 0;\
641  if (buflen <= 0) \
642  return AVERROR(EINVAL); \
643  while (ret + 1 < maxlen) {\
644  uint8_t tmp;\
645  uint32_t ch;\
646  GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
647  if (!ch)\
648  break;\
649  PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
650  }\
651  *q = 0;\
652  return ret;\
653 }\
654 
656 GET_STR16(be, avio_rb16)
657 
658 #undef GET_STR16
659 
660 uint64_t avio_rb64(AVIOContext *s)
661 {
662  uint64_t val;
663  val = (uint64_t)avio_rb32(s) << 32;
664  val |= (uint64_t)avio_rb32(s);
665  return val;
666 }
667 
669  uint64_t val = 0;
670  int tmp;
671 
672  do{
673  tmp = avio_r8(bc);
674  val= (val<<7) + (tmp&127);
675  }while(tmp&128);
676  return val;
677 }
678 
680 {
681  uint8_t *buffer;
682  int buffer_size, max_packet_size;
683 
684  max_packet_size = h->max_packet_size;
685  if (max_packet_size) {
686  buffer_size = max_packet_size; /* no need to bufferize more than one packet */
687  } else {
688  buffer_size = IO_BUFFER_SIZE;
689  }
690  buffer = av_malloc(buffer_size);
691  if (!buffer)
692  return AVERROR(ENOMEM);
693 
694  *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
696  if (!*s) {
697  av_free(buffer);
698  return AVERROR(ENOMEM);
699  }
700  (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
701  (*s)->max_packet_size = max_packet_size;
702  if(h->prot) {
703  (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
704  (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
705  }
706  (*s)->av_class = &ffio_url_class;
707  return 0;
708 }
709 
710 int ffio_set_buf_size(AVIOContext *s, int buf_size)
711 {
712  uint8_t *buffer;
713  buffer = av_malloc(buf_size);
714  if (!buffer)
715  return AVERROR(ENOMEM);
716 
717  av_free(s->buffer);
718  s->buffer = buffer;
719  s->buffer_size = buf_size;
720  s->buf_ptr = buffer;
722  return 0;
723 }
724 
725 static int url_resetbuf(AVIOContext *s, int flags)
726 {
727  assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
728 
729  if (flags & AVIO_FLAG_WRITE) {
730  s->buf_end = s->buffer + s->buffer_size;
731  s->write_flag = 1;
732  } else {
733  s->buf_end = s->buffer;
734  s->write_flag = 0;
735  }
736  return 0;
737 }
738 
739 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
740 {
741  int64_t buffer_start;
742  int buffer_size;
743  int overlap, new_size, alloc_size;
744 
745  if (s->write_flag)
746  return AVERROR(EINVAL);
747 
748  buffer_size = s->buf_end - s->buffer;
749 
750  /* the buffers must touch or overlap */
751  if ((buffer_start = s->pos - buffer_size) > buf_size)
752  return AVERROR(EINVAL);
753 
754  overlap = buf_size - buffer_start;
755  new_size = buf_size + buffer_size - overlap;
756 
757  alloc_size = FFMAX(s->buffer_size, new_size);
758  if (alloc_size > buf_size)
759  if (!(buf = av_realloc(buf, alloc_size)))
760  return AVERROR(ENOMEM);
761 
762  if (new_size > buf_size) {
763  memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
764  buf_size = new_size;
765  }
766 
767  av_free(s->buffer);
768  s->buf_ptr = s->buffer = buf;
769  s->buffer_size = alloc_size;
770  s->pos = buf_size;
771  s->buf_end = s->buf_ptr + buf_size;
772  s->eof_reached = 0;
773  s->must_flush = 0;
774 
775  return 0;
776 }
777 
778 int avio_open(AVIOContext **s, const char *filename, int flags)
779 {
780  return avio_open2(s, filename, flags, NULL, NULL);
781 }
782 
783 int avio_open2(AVIOContext **s, const char *filename, int flags,
785 {
786  URLContext *h;
787  int err;
788 
789  err = ffurl_open(&h, filename, flags, int_cb, options);
790  if (err < 0)
791  return err;
792  err = ffio_fdopen(s, h);
793  if (err < 0) {
794  ffurl_close(h);
795  return err;
796  }
797  return 0;
798 }
799 
801 {
802  URLContext *h;
803 
804  if (!s)
805  return 0;
806 
807  avio_flush(s);
808  h = s->opaque;
809  av_freep(&s->buffer);
810  av_free(s);
811  return ffurl_close(h);
812 }
813 
815 {
816  int ret = avio_close(*s);
817  *s = NULL;
818  return ret;
819 }
820 
821 int avio_printf(AVIOContext *s, const char *fmt, ...)
822 {
823  va_list ap;
824  char buf[4096];
825  int ret;
826 
827  va_start(ap, fmt);
828  ret = vsnprintf(buf, sizeof(buf), fmt, ap);
829  va_end(ap);
830  avio_write(s, buf, strlen(buf));
831  return ret;
832 }
833 
834 int avio_pause(AVIOContext *s, int pause)
835 {
836  if (!s->read_pause)
837  return AVERROR(ENOSYS);
838  return s->read_pause(s->opaque, pause);
839 }
840 
841 int64_t avio_seek_time(AVIOContext *s, int stream_index,
842  int64_t timestamp, int flags)
843 {
844  URLContext *h = s->opaque;
845  int64_t ret;
846  if (!s->read_seek)
847  return AVERROR(ENOSYS);
848  ret = s->read_seek(h, stream_index, timestamp, flags);
849  if (ret >= 0) {
850  int64_t pos;
851  s->buf_ptr = s->buf_end; // Flush buffer
852  pos = s->seek(h, 0, SEEK_CUR);
853  if (pos >= 0)
854  s->pos = pos;
855  else if (pos != AVERROR(ENOSYS))
856  ret = pos;
857  }
858  return ret;
859 }
860 
861 /* output in a dynamic buffer */
862 
863 typedef struct DynBuffer {
868 } DynBuffer;
869 
870 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
871 {
872  DynBuffer *d = opaque;
873  unsigned new_size, new_allocated_size;
874 
875  /* reallocate buffer if needed */
876  new_size = d->pos + buf_size;
877  new_allocated_size = d->allocated_size;
878  if (new_size < d->pos || new_size > INT_MAX/2)
879  return -1;
880  while (new_size > new_allocated_size) {
881  if (!new_allocated_size)
882  new_allocated_size = new_size;
883  else
884  new_allocated_size += new_allocated_size / 2 + 1;
885  }
886 
887  if (new_allocated_size > d->allocated_size) {
888  int err;
889  if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
890  d->allocated_size = 0;
891  d->size = 0;
892  return err;
893  }
894  d->allocated_size = new_allocated_size;
895  }
896  memcpy(d->buffer + d->pos, buf, buf_size);
897  d->pos = new_size;
898  if (d->pos > d->size)
899  d->size = d->pos;
900  return buf_size;
901 }
902 
903 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
904 {
905  unsigned char buf1[4];
906  int ret;
907 
908  /* packetized write: output the header */
909  AV_WB32(buf1, buf_size);
910  ret = dyn_buf_write(opaque, buf1, 4);
911  if (ret < 0)
912  return ret;
913 
914  /* then the data */
915  return dyn_buf_write(opaque, buf, buf_size);
916 }
917 
918 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
919 {
920  DynBuffer *d = opaque;
921 
922  if (whence == SEEK_CUR)
923  offset += d->pos;
924  else if (whence == SEEK_END)
925  offset += d->size;
926  if (offset < 0 || offset > 0x7fffffffLL)
927  return -1;
928  d->pos = offset;
929  return 0;
930 }
931 
932 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
933 {
934  DynBuffer *d;
935  unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
936 
937  if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
938  return -1;
939  d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
940  if (!d)
941  return AVERROR(ENOMEM);
942  d->io_buffer_size = io_buffer_size;
943  *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
944  max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
945  max_packet_size ? NULL : dyn_buf_seek);
946  if(!*s) {
947  av_free(d);
948  return AVERROR(ENOMEM);
949  }
950  (*s)->max_packet_size = max_packet_size;
951  return 0;
952 }
953 
955 {
956  return url_open_dyn_buf_internal(s, 0);
957 }
958 
959 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
960 {
961  if (max_packet_size <= 0)
962  return -1;
963  return url_open_dyn_buf_internal(s, max_packet_size);
964 }
965 
967 {
968  DynBuffer *d;
969  int size;
970  static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
971  int padding = 0;
972 
973  if (!s) {
974  *pbuffer = NULL;
975  return 0;
976  }
977 
978  /* don't attempt to pad fixed-size packet buffers */
979  if (!s->max_packet_size) {
980  avio_write(s, padbuf, sizeof(padbuf));
982  }
983 
984  avio_flush(s);
985 
986  d = s->opaque;
987  *pbuffer = d->buffer;
988  size = d->size;
989  av_free(d);
990  av_free(s);
991  return size - padding;
992 }
993 
994 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
995 {
996  DynBuffer *d = opaque;
997 
998  d->pos += buf_size;
999  if (d->pos > d->size)
1000  d->size = d->pos;
1001  return buf_size;
1002 }
1003 
1005 {
1006  int ret = url_open_dyn_buf_internal(s, 0);
1007  if (ret >= 0) {
1008  AVIOContext *pb = *s;
1010  }
1011  return ret;
1012 }
1013 
1015 {
1016  DynBuffer *d = s->opaque;
1017  int size;
1018 
1019  avio_flush(s);
1020 
1021  size = d->size;
1022  av_free(d);
1023  av_free(s);
1024  return size;
1025 }
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:324
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:918
Bytestream IO Context.
Definition: avio.h:68
Buffered I/O operations.
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:660
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:954
#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:867
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:342
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
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:511
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:151
#define AVIO_FLAG_READ
read-only
Definition: avio.h:292
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:556
struct URLProtocol * prot
Definition: url.h:43
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:293
unsigned char * buffer
Start of the buffer.
Definition: avio.h:82
void avio_wl24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:348
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:814
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:268
#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:276
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
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:362
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:423
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:1004
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:710
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:499
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:354
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:336
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:866
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:588
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:903
#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:548
int avio_pause(AVIOContext *s, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e.g.
Definition: aviobuf.c:834
#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:564
int size
Definition: aviobuf.c:864
int(* read_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:91
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:966
unsigned char * checksum_ptr
Definition: avio.h:100
int avio_printf(AVIOContext *s, const char *fmt,...)
Definition: aviobuf.c:821
int64_t(* seek)(void *opaque, int64_t offset, int whence)
Definition: avio.h:93
int allocated_size
Definition: aviobuf.c:864
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:870
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:603
#define GET_STR16(type, read)
Definition: aviobuf.c:636
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:330
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
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:959
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:778
av_default_item_name
Definition: dnxhdenc.c:52
static int url_resetbuf(AVIOContext *s, int flags)
Definition: aviobuf.c:725
int(* url_read_pause)(URLContext *h, int pause)
Definition: url.h:80
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:572
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:800
int(* write_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:92
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:580
int pos
Definition: aviobuf.c:864
#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:739
int ff_get_v_length(uint64_t val)
Get the length in bytes which is needed to store val as v.
Definition: aviobuf.c:304
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:783
int(* read_pause)(void *opaque, int pause)
Pause or resume playback for network streaming protocols - e.g.
Definition: avio.h:106
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:618
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
unsigned long(* update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size)
Definition: avio.h:101
int ffio_fdopen(AVIOContext **s, URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h...
Definition: aviobuf.c:679
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:431
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:668
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
uint8_t * buffer
Definition: aviobuf.c:865
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:314
int len
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1014
static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:994
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
Definition: aviobuf.c:932
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:287
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:411
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
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
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
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:841
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:417
int64_t(* url_read_seek)(URLContext *h, int stream_index, int64_t timestamp, int flags)
Definition: url.h:81