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 typedef struct AVIOInternal {
47 } AVIOInternal;
48 
49 static void *ff_avio_child_next(void *obj, void *prev)
50 {
51  AVIOContext *s = obj;
52  AVIOInternal *internal = s->opaque;
53  return prev ? NULL : internal->h;
54 }
55 
56 static const AVClass *ff_avio_child_class_next(const AVClass *prev)
57 {
58  return prev ? NULL : &ffurl_context_class;
59 }
60 
61 #define OFFSET(x) offsetof(AVIOContext,x)
62 #define E AV_OPT_FLAG_ENCODING_PARAM
63 #define D AV_OPT_FLAG_DECODING_PARAM
64 static const AVOption ff_avio_options[] = {
65  {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, D },
66  { NULL },
67 };
68 
70  .class_name = "AVIOContext",
71  .item_name = av_default_item_name,
72  .version = LIBAVUTIL_VERSION_INT,
73  .option = ff_avio_options,
74  .child_next = ff_avio_child_next,
75  .child_class_next = ff_avio_child_class_next,
76 };
77 
78 static void fill_buffer(AVIOContext *s);
79 static int url_resetbuf(AVIOContext *s, int flags);
80 
82  unsigned char *buffer,
83  int buffer_size,
84  int write_flag,
85  void *opaque,
86  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
87  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
88  int64_t (*seek)(void *opaque, int64_t offset, int whence))
89 {
90  s->buffer = buffer;
91  s->orig_buffer_size =
92  s->buffer_size = buffer_size;
93  s->buf_ptr = buffer;
94  s->buf_ptr_max = buffer;
95  s->opaque = opaque;
96  s->direct = 0;
97 
98  url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
99 
102  s->seek = seek;
103  s->pos = 0;
104  s->eof_reached = 0;
105  s->error = 0;
106  s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
107  s->min_packet_size = 0;
108  s->max_packet_size = 0;
109  s->update_checksum = NULL;
111 
112  if (!read_packet && !write_flag) {
113  s->pos = buffer_size;
114  s->buf_end = s->buffer + buffer_size;
115  }
116  s->read_pause = NULL;
117  s->read_seek = NULL;
118 
119  s->write_data_type = NULL;
120  s->ignore_boundary_point = 0;
123  s->short_seek_get = NULL;
124  s->written = 0;
125 
126  return 0;
127 }
128 
130  unsigned char *buffer,
131  int buffer_size,
132  int write_flag,
133  void *opaque,
134  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
135  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
136  int64_t (*seek)(void *opaque, int64_t offset, int whence))
137 {
138  AVIOContext *s = av_mallocz(sizeof(AVIOContext));
139  if (!s)
140  return NULL;
141  ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
142  read_packet, write_packet, seek);
143  return s;
144 }
145 
147 {
148  av_freep(ps);
149 }
150 
151 static void writeout(AVIOContext *s, const uint8_t *data, int len)
152 {
153  if (!s->error) {
154  int ret = 0;
155  if (s->write_data_type)
156  ret = s->write_data_type(s->opaque, (uint8_t *)data,
157  len,
158  s->current_type,
159  s->last_time);
160  else if (s->write_packet)
161  ret = s->write_packet(s->opaque, (uint8_t *)data, len);
162  if (ret < 0) {
163  s->error = ret;
164  } else {
165  if (s->pos + len > s->written)
166  s->written = s->pos + len;
167  }
168  }
172  }
174  s->writeout_count ++;
175  s->pos += len;
176 }
177 
178 static void flush_buffer(AVIOContext *s)
179 {
180  s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
181  if (s->write_flag && s->buf_ptr_max > s->buffer) {
182  writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
183  if (s->update_checksum) {
185  s->buf_ptr_max - s->checksum_ptr);
186  s->checksum_ptr = s->buffer;
187  }
188  }
189  s->buf_ptr = s->buf_ptr_max = s->buffer;
190  if (!s->write_flag)
191  s->buf_end = s->buffer;
192 }
193 
194 void avio_w8(AVIOContext *s, int b)
195 {
196  av_assert2(b>=-128 && b<=255);
197  *s->buf_ptr++ = b;
198  if (s->buf_ptr >= s->buf_end)
199  flush_buffer(s);
200 }
201 
202 void ffio_fill(AVIOContext *s, int b, int count)
203 {
204  while (count > 0) {
205  int len = FFMIN(s->buf_end - s->buf_ptr, count);
206  memset(s->buf_ptr, b, len);
207  s->buf_ptr += len;
208 
209  if (s->buf_ptr >= s->buf_end)
210  flush_buffer(s);
211 
212  count -= len;
213  }
214 }
215 
216 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
217 {
218  if (s->direct && !s->update_checksum) {
219  avio_flush(s);
220  writeout(s, buf, size);
221  return;
222  }
223  while (size > 0) {
224  int len = FFMIN(s->buf_end - s->buf_ptr, size);
225  memcpy(s->buf_ptr, buf, len);
226  s->buf_ptr += len;
227 
228  if (s->buf_ptr >= s->buf_end)
229  flush_buffer(s);
230 
231  buf += len;
232  size -= len;
233  }
234 }
235 
237 {
238  int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;
239  flush_buffer(s);
240  if (seekback)
241  avio_seek(s, seekback, SEEK_CUR);
242 }
243 
244 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
245 {
246  int64_t offset1;
247  int64_t pos;
248  int force = whence & AVSEEK_FORCE;
249  int buffer_size;
250  int short_seek;
251  whence &= ~AVSEEK_FORCE;
252 
253  if(!s)
254  return AVERROR(EINVAL);
255 
256  buffer_size = s->buf_end - s->buffer;
257  // pos is the absolute position that the beginning of s->buffer corresponds to in the file
258  pos = s->pos - (s->write_flag ? 0 : buffer_size);
259 
260  if (whence != SEEK_CUR && whence != SEEK_SET)
261  return AVERROR(EINVAL);
262 
263  if (whence == SEEK_CUR) {
264  offset1 = pos + (s->buf_ptr - s->buffer);
265  if (offset == 0)
266  return offset1;
267  if (offset > INT64_MAX - offset1)
268  return AVERROR(EINVAL);
269  offset += offset1;
270  }
271  if (offset < 0)
272  return AVERROR(EINVAL);
273 
274  if (s->short_seek_get) {
275  short_seek = s->short_seek_get(s->opaque);
276  /* fallback to default short seek */
277  if (short_seek <= 0)
278  short_seek = s->short_seek_threshold;
279  } else
280  short_seek = s->short_seek_threshold;
281 
282  offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
283  s->buf_ptr_max = FFMAX(s->buf_ptr_max, s->buf_ptr);
284  if ((!s->direct || !s->seek) &&
285  offset1 >= 0 && offset1 <= (s->write_flag ? s->buf_ptr_max - s->buffer : buffer_size)) {
286  /* can do the seek inside the buffer */
287  s->buf_ptr = s->buffer + offset1;
288  } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
289  offset1 <= buffer_size + short_seek) &&
290  !s->write_flag && offset1 >= 0 &&
291  (!s->direct || !s->seek) &&
292  (whence != SEEK_END || force)) {
293  while(s->pos < offset && !s->eof_reached)
294  fill_buffer(s);
295  if (s->eof_reached)
296  return AVERROR_EOF;
297  s->buf_ptr = s->buf_end - (s->pos - offset);
298  } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
299  int64_t res;
300 
301  pos -= FFMIN(buffer_size>>1, pos);
302  if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
303  return res;
304  s->buf_end =
305  s->buf_ptr = s->buffer;
306  s->pos = pos;
307  s->eof_reached = 0;
308  fill_buffer(s);
309  return avio_seek(s, offset, SEEK_SET | force);
310  } else {
311  int64_t res;
312  if (s->write_flag) {
313  flush_buffer(s);
314  }
315  if (!s->seek)
316  return AVERROR(EPIPE);
317  if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
318  return res;
319  s->seek_count ++;
320  if (!s->write_flag)
321  s->buf_end = s->buffer;
322  s->buf_ptr = s->buf_ptr_max = s->buffer;
323  s->pos = offset;
324  }
325  s->eof_reached = 0;
326  return offset;
327 }
328 
329 int64_t avio_skip(AVIOContext *s, int64_t offset)
330 {
331  return avio_seek(s, offset, SEEK_CUR);
332 }
333 
335 {
336  int64_t size;
337 
338  if (!s)
339  return AVERROR(EINVAL);
340 
341  if (s->written)
342  return s->written;
343 
344  if (!s->seek)
345  return AVERROR(ENOSYS);
346  size = s->seek(s->opaque, 0, AVSEEK_SIZE);
347  if (size < 0) {
348  if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
349  return size;
350  size++;
351  s->seek(s->opaque, s->pos, SEEK_SET);
352  }
353  return size;
354 }
355 
357 {
358  if(!s)
359  return 0;
360  if(s->eof_reached){
361  s->eof_reached=0;
362  fill_buffer(s);
363  }
364  return s->eof_reached;
365 }
366 
367 void avio_wl32(AVIOContext *s, unsigned int val)
368 {
369  avio_w8(s, (uint8_t) val );
370  avio_w8(s, (uint8_t)(val >> 8 ));
371  avio_w8(s, (uint8_t)(val >> 16));
372  avio_w8(s, val >> 24 );
373 }
374 
375 void avio_wb32(AVIOContext *s, unsigned int val)
376 {
377  avio_w8(s, val >> 24 );
378  avio_w8(s, (uint8_t)(val >> 16));
379  avio_w8(s, (uint8_t)(val >> 8 ));
380  avio_w8(s, (uint8_t) val );
381 }
382 
383 int avio_put_str(AVIOContext *s, const char *str)
384 {
385  int len = 1;
386  if (str) {
387  len += strlen(str);
388  avio_write(s, (const unsigned char *) str, len);
389  } else
390  avio_w8(s, 0);
391  return len;
392 }
393 
394 static inline int put_str16(AVIOContext *s, const char *str, const int be)
395 {
396  const uint8_t *q = str;
397  int ret = 0;
398  int err = 0;
399 
400  while (*q) {
401  uint32_t ch;
402  uint16_t tmp;
403 
404  GET_UTF8(ch, *q++, goto invalid;)
405  PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
406  ret += 2;)
407  continue;
408 invalid:
409  av_log(s, AV_LOG_ERROR, "Invalid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
410  err = AVERROR(EINVAL);
411  if (!*(q-1))
412  break;
413  }
414  if (be)
415  avio_wb16(s, 0);
416  else
417  avio_wl16(s, 0);
418  if (err)
419  return err;
420  ret += 2;
421  return ret;
422 }
423 
424 #define PUT_STR16(type, big_endian) \
425 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
426 { \
427 return put_str16(s, str, big_endian); \
428 }
429 
430 PUT_STR16(le, 0)
431 PUT_STR16(be, 1)
432 
433 #undef PUT_STR16
434 
435 int ff_get_v_length(uint64_t val)
436 {
437  int i = 1;
438 
439  while (val >>= 7)
440  i++;
441 
442  return i;
443 }
444 
445 void ff_put_v(AVIOContext *bc, uint64_t val)
446 {
447  int i = ff_get_v_length(val);
448 
449  while (--i > 0)
450  avio_w8(bc, 128 | (uint8_t)(val >> (7*i)));
451 
452  avio_w8(bc, val & 127);
453 }
454 
455 void avio_wl64(AVIOContext *s, uint64_t val)
456 {
457  avio_wl32(s, (uint32_t)(val & 0xffffffff));
458  avio_wl32(s, (uint32_t)(val >> 32));
459 }
460 
461 void avio_wb64(AVIOContext *s, uint64_t val)
462 {
463  avio_wb32(s, (uint32_t)(val >> 32));
464  avio_wb32(s, (uint32_t)(val & 0xffffffff));
465 }
466 
467 void avio_wl16(AVIOContext *s, unsigned int val)
468 {
469  avio_w8(s, (uint8_t)val);
470  avio_w8(s, (int)val >> 8);
471 }
472 
473 void avio_wb16(AVIOContext *s, unsigned int val)
474 {
475  avio_w8(s, (int)val >> 8);
476  avio_w8(s, (uint8_t)val);
477 }
478 
479 void avio_wl24(AVIOContext *s, unsigned int val)
480 {
481  avio_wl16(s, val & 0xffff);
482  avio_w8(s, (int)val >> 16);
483 }
484 
485 void avio_wb24(AVIOContext *s, unsigned int val)
486 {
487  avio_wb16(s, (int)val >> 8);
488  avio_w8(s, (uint8_t)val);
489 }
490 
492 {
493  if (type == AVIO_DATA_MARKER_FLUSH_POINT) {
494  if (s->buf_ptr - s->buffer >= s->min_packet_size)
495  avio_flush(s);
496  return;
497  }
498  if (!s->write_data_type)
499  return;
500  // If ignoring boundary points, just treat it as unknown
503  // Avoid unnecessary flushes if we are already in non-header/trailer
504  // data and setting the type to unknown
505  if (type == AVIO_DATA_MARKER_UNKNOWN &&
508  return;
509 
510  switch (type) {
513  // For header/trailer, ignore a new marker of the same type;
514  // consecutive header/trailer markers can be merged.
515  if (type == s->current_type)
516  return;
517  break;
518  }
519 
520  // If we've reached here, we have a new, noteworthy marker.
521  // Flush the previous data and mark the start of the new data.
522  avio_flush(s);
523  s->current_type = type;
524  s->last_time = time;
525 }
526 
527 static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
528 {
529  int ret;
530 
531  if (!s->read_packet)
532  return AVERROR(EINVAL);
533  ret = s->read_packet(s->opaque, buf, size);
534 #if FF_API_OLD_AVIO_EOF_0
535  if (!ret && !s->max_packet_size) {
536  av_log(NULL, AV_LOG_WARNING, "Invalid return value 0 for stream protocol\n");
537  ret = AVERROR_EOF;
538  }
539 #else
540  av_assert2(ret || s->max_packet_size);
541 #endif
542  return ret;
543 }
544 
545 /* Input stream */
546 
547 static void fill_buffer(AVIOContext *s)
548 {
549  int max_buffer_size = s->max_packet_size ?
551  uint8_t *dst = s->buf_end - s->buffer + max_buffer_size < s->buffer_size ?
552  s->buf_end : s->buffer;
553  int len = s->buffer_size - (dst - s->buffer);
554 
555  /* can't fill the buffer without read_packet, just set EOF if appropriate */
556  if (!s->read_packet && s->buf_ptr >= s->buf_end)
557  s->eof_reached = 1;
558 
559  /* no need to do anything if EOF already reached */
560  if (s->eof_reached)
561  return;
562 
563  if (s->update_checksum && dst == s->buffer) {
564  if (s->buf_end > s->checksum_ptr)
566  s->buf_end - s->checksum_ptr);
567  s->checksum_ptr = s->buffer;
568  }
569 
570  /* make buffer smaller in case it ended up large after probing */
571  if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size) {
572  if (dst == s->buffer && s->buf_ptr != dst) {
573  int ret = ffio_set_buf_size(s, s->orig_buffer_size);
574  if (ret < 0)
575  av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
576 
577  s->checksum_ptr = dst = s->buffer;
578  }
579  av_assert0(len >= s->orig_buffer_size);
580  len = s->orig_buffer_size;
581  }
582 
583  len = read_packet_wrapper(s, dst, len);
584  if (len == AVERROR_EOF) {
585  /* do not modify buffer if EOF reached so that a seek back can
586  be done without rereading data */
587  s->eof_reached = 1;
588  } else if (len < 0) {
589  s->eof_reached = 1;
590  s->error= len;
591  } else {
592  s->pos += len;
593  s->buf_ptr = dst;
594  s->buf_end = dst + len;
595  s->bytes_read += len;
596  }
597 }
598 
599 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
600  unsigned int len)
601 {
602  return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
603 }
604 
605 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
606  unsigned int len)
607 {
608  return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
609 }
610 
611 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
612  unsigned int len)
613 {
614  return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
615 }
616 
618 {
620  s->buf_ptr - s->checksum_ptr);
621  s->update_checksum = NULL;
622  return s->checksum;
623 }
624 
626  unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
627  unsigned long checksum)
628 {
629  s->update_checksum = update_checksum;
630  if (s->update_checksum) {
631  s->checksum = checksum;
632  s->checksum_ptr = s->buf_ptr;
633  }
634 }
635 
636 /* XXX: put an inline version */
638 {
639  if (s->buf_ptr >= s->buf_end)
640  fill_buffer(s);
641  if (s->buf_ptr < s->buf_end)
642  return *s->buf_ptr++;
643  return 0;
644 }
645 
646 int avio_read(AVIOContext *s, unsigned char *buf, int size)
647 {
648  int len, size1;
649 
650  size1 = size;
651  while (size > 0) {
652  len = FFMIN(s->buf_end - s->buf_ptr, size);
653  if (len == 0 || s->write_flag) {
654  if((s->direct || size > s->buffer_size) && !s->update_checksum) {
655  // bypass the buffer and read data directly into buf
656  len = read_packet_wrapper(s, buf, size);
657  if (len == AVERROR_EOF) {
658  /* do not modify buffer if EOF reached so that a seek back can
659  be done without rereading data */
660  s->eof_reached = 1;
661  break;
662  } else if (len < 0) {
663  s->eof_reached = 1;
664  s->error= len;
665  break;
666  } else {
667  s->pos += len;
668  s->bytes_read += len;
669  size -= len;
670  buf += len;
671  // reset the buffer
672  s->buf_ptr = s->buffer;
673  s->buf_end = s->buffer/* + len*/;
674  }
675  } else {
676  fill_buffer(s);
677  len = s->buf_end - s->buf_ptr;
678  if (len == 0)
679  break;
680  }
681  } else {
682  memcpy(buf, s->buf_ptr, len);
683  buf += len;
684  s->buf_ptr += len;
685  size -= len;
686  }
687  }
688  if (size1 == size) {
689  if (s->error) return s->error;
690  if (avio_feof(s)) return AVERROR_EOF;
691  }
692  return size1 - size;
693 }
694 
695 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
696 {
697  int ret = avio_read(s, buf, size);
698  if (ret != size)
699  return AVERROR_INVALIDDATA;
700  return ret;
701 }
702 
703 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
704 {
705  if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
706  *data = s->buf_ptr;
707  s->buf_ptr += size;
708  return size;
709  } else {
710  *data = buf;
711  return avio_read(s, buf, size);
712  }
713 }
714 
715 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
716 {
717  int len;
718 
719  if (size < 0)
720  return -1;
721 
722  if (s->read_packet && s->write_flag) {
723  len = read_packet_wrapper(s, buf, size);
724  if (len > 0)
725  s->pos += len;
726  return len;
727  }
728 
729  len = s->buf_end - s->buf_ptr;
730  if (len == 0) {
731  /* Reset the buf_end pointer to the start of the buffer, to make sure
732  * the fill_buffer call tries to read as much data as fits into the
733  * full buffer, instead of just what space is left after buf_end.
734  * This avoids returning partial packets at the end of the buffer,
735  * for packet based inputs.
736  */
737  s->buf_end = s->buf_ptr = s->buffer;
738  fill_buffer(s);
739  len = s->buf_end - s->buf_ptr;
740  }
741  if (len > size)
742  len = size;
743  memcpy(buf, s->buf_ptr, len);
744  s->buf_ptr += len;
745  if (!len) {
746  if (s->error) return s->error;
747  if (avio_feof(s)) return AVERROR_EOF;
748  }
749  return len;
750 }
751 
752 unsigned int avio_rl16(AVIOContext *s)
753 {
754  unsigned int val;
755  val = avio_r8(s);
756  val |= avio_r8(s) << 8;
757  return val;
758 }
759 
760 unsigned int avio_rl24(AVIOContext *s)
761 {
762  unsigned int val;
763  val = avio_rl16(s);
764  val |= avio_r8(s) << 16;
765  return val;
766 }
767 
768 unsigned int avio_rl32(AVIOContext *s)
769 {
770  unsigned int val;
771  val = avio_rl16(s);
772  val |= avio_rl16(s) << 16;
773  return val;
774 }
775 
776 uint64_t avio_rl64(AVIOContext *s)
777 {
778  uint64_t val;
779  val = (uint64_t)avio_rl32(s);
780  val |= (uint64_t)avio_rl32(s) << 32;
781  return val;
782 }
783 
784 unsigned int avio_rb16(AVIOContext *s)
785 {
786  unsigned int val;
787  val = avio_r8(s) << 8;
788  val |= avio_r8(s);
789  return val;
790 }
791 
792 unsigned int avio_rb24(AVIOContext *s)
793 {
794  unsigned int val;
795  val = avio_rb16(s) << 8;
796  val |= avio_r8(s);
797  return val;
798 }
799 unsigned int avio_rb32(AVIOContext *s)
800 {
801  unsigned int val;
802  val = avio_rb16(s) << 16;
803  val |= avio_rb16(s);
804  return val;
805 }
806 
807 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
808 {
809  int i = 0;
810  char c;
811 
812  do {
813  c = avio_r8(s);
814  if (c && i < maxlen-1)
815  buf[i++] = c;
816  } while (c != '\n' && c != '\r' && c);
817  if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
818  avio_skip(s, -1);
819 
820  buf[i] = 0;
821  return i;
822 }
823 
824 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
825 {
826  int i;
827 
828  if (buflen <= 0)
829  return AVERROR(EINVAL);
830  // reserve 1 byte for terminating 0
831  buflen = FFMIN(buflen - 1, maxlen);
832  for (i = 0; i < buflen; i++)
833  if (!(buf[i] = avio_r8(s)))
834  return i + 1;
835  buf[i] = 0;
836  for (; i < maxlen; i++)
837  if (!avio_r8(s))
838  return i + 1;
839  return maxlen;
840 }
841 
842 #define GET_STR16(type, read) \
843  int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
844 {\
845  char* q = buf;\
846  int ret = 0;\
847  if (buflen <= 0) \
848  return AVERROR(EINVAL); \
849  while (ret + 1 < maxlen) {\
850  uint8_t tmp;\
851  uint32_t ch;\
852  GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
853  if (!ch)\
854  break;\
855  PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
856  }\
857  *q = 0;\
858  return ret;\
859 }\
860 
861 GET_STR16(le, avio_rl16)
862 GET_STR16(be, avio_rb16)
863 
864 #undef GET_STR16
865 
866 uint64_t avio_rb64(AVIOContext *s)
867 {
868  uint64_t val;
869  val = (uint64_t)avio_rb32(s) << 32;
870  val |= (uint64_t)avio_rb32(s);
871  return val;
872 }
873 
875  uint64_t val = 0;
876  int tmp;
877 
878  do{
879  tmp = avio_r8(bc);
880  val= (val<<7) + (tmp&127);
881  }while(tmp&128);
882  return val;
883 }
884 
885 static int io_read_packet(void *opaque, uint8_t *buf, int buf_size)
886 {
887  AVIOInternal *internal = opaque;
888  return ffurl_read(internal->h, buf, buf_size);
889 }
890 
891 static int io_write_packet(void *opaque, uint8_t *buf, int buf_size)
892 {
893  AVIOInternal *internal = opaque;
894  return ffurl_write(internal->h, buf, buf_size);
895 }
896 
897 static int64_t io_seek(void *opaque, int64_t offset, int whence)
898 {
899  AVIOInternal *internal = opaque;
900  return ffurl_seek(internal->h, offset, whence);
901 }
902 
903 static int io_short_seek(void *opaque)
904 {
905  AVIOInternal *internal = opaque;
906  return ffurl_get_short_seek(internal->h);
907 }
908 
909 static int io_read_pause(void *opaque, int pause)
910 {
911  AVIOInternal *internal = opaque;
912  if (!internal->h->prot->url_read_pause)
913  return AVERROR(ENOSYS);
914  return internal->h->prot->url_read_pause(internal->h, pause);
915 }
916 
917 static int64_t io_read_seek(void *opaque, int stream_index, int64_t timestamp, int flags)
918 {
919  AVIOInternal *internal = opaque;
920  if (!internal->h->prot->url_read_seek)
921  return AVERROR(ENOSYS);
922  return internal->h->prot->url_read_seek(internal->h, stream_index, timestamp, flags);
923 }
924 
926 {
927  AVIOInternal *internal = NULL;
928  uint8_t *buffer = NULL;
929  int buffer_size, max_packet_size;
930 
931  max_packet_size = h->max_packet_size;
932  if (max_packet_size) {
933  buffer_size = max_packet_size; /* no need to bufferize more than one packet */
934  } else {
935  buffer_size = IO_BUFFER_SIZE;
936  }
937  buffer = av_malloc(buffer_size);
938  if (!buffer)
939  return AVERROR(ENOMEM);
940 
941  internal = av_mallocz(sizeof(*internal));
942  if (!internal)
943  goto fail;
944 
945  internal->h = h;
946 
947  *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE,
949  if (!*s)
950  goto fail;
951 
952  (*s)->protocol_whitelist = av_strdup(h->protocol_whitelist);
953  if (!(*s)->protocol_whitelist && h->protocol_whitelist) {
954  avio_closep(s);
955  goto fail;
956  }
957  (*s)->protocol_blacklist = av_strdup(h->protocol_blacklist);
958  if (!(*s)->protocol_blacklist && h->protocol_blacklist) {
959  avio_closep(s);
960  goto fail;
961  }
962  (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
963 
964  (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
965  (*s)->max_packet_size = max_packet_size;
966  (*s)->min_packet_size = h->min_packet_size;
967  if(h->prot) {
968  (*s)->read_pause = io_read_pause;
969  (*s)->read_seek = io_read_seek;
970 
971  if (h->prot->url_read_seek)
972  (*s)->seekable |= AVIO_SEEKABLE_TIME;
973  }
974  (*s)->short_seek_get = io_short_seek;
975  (*s)->av_class = &ff_avio_class;
976  return 0;
977 fail:
978  av_freep(&internal);
979  av_freep(&buffer);
980  return AVERROR(ENOMEM);
981 }
982 
983 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
984 {
985  uint8_t *buffer;
986  int max_buffer_size = s->max_packet_size ?
988  int filled = s->buf_end - s->buffer;
989  ptrdiff_t checksum_ptr_offset = s->checksum_ptr ? s->checksum_ptr - s->buffer : -1;
990 
991  buf_size += s->buf_ptr - s->buffer + max_buffer_size;
992 
993  if (buf_size < filled || s->seekable || !s->read_packet)
994  return 0;
995  av_assert0(!s->write_flag);
996 
997  buffer = av_malloc(buf_size);
998  if (!buffer)
999  return AVERROR(ENOMEM);
1000 
1001  memcpy(buffer, s->buffer, filled);
1002  av_free(s->buffer);
1003  s->buf_ptr = buffer + (s->buf_ptr - s->buffer);
1004  s->buf_end = buffer + (s->buf_end - s->buffer);
1005  s->buffer = buffer;
1006  s->buffer_size = buf_size;
1007  if (checksum_ptr_offset >= 0)
1008  s->checksum_ptr = s->buffer + checksum_ptr_offset;
1009  return 0;
1010 }
1011 
1012 int ffio_set_buf_size(AVIOContext *s, int buf_size)
1013 {
1014  uint8_t *buffer;
1015  buffer = av_malloc(buf_size);
1016  if (!buffer)
1017  return AVERROR(ENOMEM);
1018 
1019  av_free(s->buffer);
1020  s->buffer = buffer;
1021  s->orig_buffer_size =
1022  s->buffer_size = buf_size;
1023  s->buf_ptr = s->buf_ptr_max = buffer;
1025  return 0;
1026 }
1027 
1028 static int url_resetbuf(AVIOContext *s, int flags)
1029 {
1030  av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
1031 
1032  if (flags & AVIO_FLAG_WRITE) {
1033  s->buf_end = s->buffer + s->buffer_size;
1034  s->write_flag = 1;
1035  } else {
1036  s->buf_end = s->buffer;
1037  s->write_flag = 0;
1038  }
1039  return 0;
1040 }
1041 
1042 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
1043 {
1044  int64_t buffer_start;
1045  int buffer_size;
1046  int overlap, new_size, alloc_size;
1047  uint8_t *buf = *bufp;
1048 
1049  if (s->write_flag) {
1050  av_freep(bufp);
1051  return AVERROR(EINVAL);
1052  }
1053 
1054  buffer_size = s->buf_end - s->buffer;
1055 
1056  /* the buffers must touch or overlap */
1057  if ((buffer_start = s->pos - buffer_size) > buf_size) {
1058  av_freep(bufp);
1059  return AVERROR(EINVAL);
1060  }
1061 
1062  overlap = buf_size - buffer_start;
1063  new_size = buf_size + buffer_size - overlap;
1064 
1065  alloc_size = FFMAX(s->buffer_size, new_size);
1066  if (alloc_size > buf_size)
1067  if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
1068  return AVERROR(ENOMEM);
1069 
1070  if (new_size > buf_size) {
1071  memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
1072  buf_size = new_size;
1073  }
1074 
1075  av_free(s->buffer);
1076  s->buf_ptr = s->buffer = buf;
1077  s->buffer_size = alloc_size;
1078  s->pos = buf_size;
1079  s->buf_end = s->buf_ptr + buf_size;
1080  s->eof_reached = 0;
1081 
1082  return 0;
1083 }
1084 
1085 int avio_open(AVIOContext **s, const char *filename, int flags)
1086 {
1087  return avio_open2(s, filename, flags, NULL, NULL);
1088 }
1089 
1090 int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
1092  const char *whitelist, const char *blacklist
1093  )
1094 {
1095  URLContext *h;
1096  int err;
1097 
1098  err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
1099  if (err < 0)
1100  return err;
1101  err = ffio_fdopen(s, h);
1102  if (err < 0) {
1103  ffurl_close(h);
1104  return err;
1105  }
1106  return 0;
1107 }
1108 
1109 int avio_open2(AVIOContext **s, const char *filename, int flags,
1111 {
1112  return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
1113 }
1114 
1115 int ffio_open2_wrapper(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags,
1117 {
1118  return ffio_open_whitelist(pb, url, flags, int_cb, options, s->protocol_whitelist, s->protocol_blacklist);
1119 }
1120 
1122 {
1123  AVIOInternal *internal;
1124  URLContext *h;
1125 
1126  if (!s)
1127  return 0;
1128 
1129  avio_flush(s);
1130  internal = s->opaque;
1131  h = internal->h;
1132 
1133  av_freep(&s->opaque);
1134  av_freep(&s->buffer);
1135  if (s->write_flag)
1136  av_log(s, AV_LOG_DEBUG, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
1137  else
1138  av_log(s, AV_LOG_DEBUG, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
1139  av_opt_free(s);
1140 
1141  avio_context_free(&s);
1142 
1143  return ffurl_close(h);
1144 }
1145 
1147 {
1148  int ret = avio_close(*s);
1149  *s = NULL;
1150  return ret;
1151 }
1152 
1153 int avio_printf(AVIOContext *s, const char *fmt, ...)
1154 {
1155  va_list ap;
1156  char buf[4096]; /* update doc entry in avio.h if changed */
1157  int ret;
1158 
1159  va_start(ap, fmt);
1160  ret = vsnprintf(buf, sizeof(buf), fmt, ap);
1161  va_end(ap);
1162  avio_write(s, buf, strlen(buf));
1163  return ret;
1164 }
1165 
1166 int avio_pause(AVIOContext *s, int pause)
1167 {
1168  if (!s->read_pause)
1169  return AVERROR(ENOSYS);
1170  return s->read_pause(s->opaque, pause);
1171 }
1172 
1173 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1174  int64_t timestamp, int flags)
1175 {
1176  int64_t ret;
1177  if (!s->read_seek)
1178  return AVERROR(ENOSYS);
1179  ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1180  if (ret >= 0) {
1181  int64_t pos;
1182  s->buf_ptr = s->buf_end; // Flush buffer
1183  pos = s->seek(s->opaque, 0, SEEK_CUR);
1184  if (pos >= 0)
1185  s->pos = pos;
1186  else if (pos != AVERROR(ENOSYS))
1187  ret = pos;
1188  }
1189  return ret;
1190 }
1191 
1192 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1193 {
1194  int ret;
1195  char buf[1024];
1196  while (max_size) {
1197  ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1198  if (ret == AVERROR_EOF)
1199  return 0;
1200  if (ret <= 0)
1201  return ret;
1202  av_bprint_append_data(pb, buf, ret);
1203  if (!av_bprint_is_complete(pb))
1204  return AVERROR(ENOMEM);
1205  max_size -= ret;
1206  }
1207  return 0;
1208 }
1209 
1211 {
1212  int ret;
1213  AVIOInternal *internal = s->opaque;
1214  URLContext *sc = internal->h;
1215  URLContext *cc = NULL;
1216  ret = ffurl_accept(sc, &cc);
1217  if (ret < 0)
1218  return ret;
1219  return ffio_fdopen(c, cc);
1220 }
1221 
1223 {
1224  AVIOInternal *internal = c->opaque;
1225  URLContext *cc = internal->h;
1226  return ffurl_handshake(cc);
1227 }
1228 
1229 /* output in a dynamic buffer */
1230 
1231 typedef struct DynBuffer {
1236 } DynBuffer;
1237 
1238 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1239 {
1240  DynBuffer *d = opaque;
1241  unsigned new_size, new_allocated_size;
1242 
1243  /* reallocate buffer if needed */
1244  new_size = d->pos + buf_size;
1245  new_allocated_size = d->allocated_size;
1246  if (new_size < d->pos || new_size > INT_MAX/2)
1247  return -1;
1248  while (new_size > new_allocated_size) {
1249  if (!new_allocated_size)
1250  new_allocated_size = new_size;
1251  else
1252  new_allocated_size += new_allocated_size / 2 + 1;
1253  }
1254 
1255  if (new_allocated_size > d->allocated_size) {
1256  int err;
1257  if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1258  d->allocated_size = 0;
1259  d->size = 0;
1260  return err;
1261  }
1262  d->allocated_size = new_allocated_size;
1263  }
1264  memcpy(d->buffer + d->pos, buf, buf_size);
1265  d->pos = new_size;
1266  if (d->pos > d->size)
1267  d->size = d->pos;
1268  return buf_size;
1269 }
1270 
1271 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1272 {
1273  unsigned char buf1[4];
1274  int ret;
1275 
1276  /* packetized write: output the header */
1277  AV_WB32(buf1, buf_size);
1278  ret = dyn_buf_write(opaque, buf1, 4);
1279  if (ret < 0)
1280  return ret;
1281 
1282  /* then the data */
1283  return dyn_buf_write(opaque, buf, buf_size);
1284 }
1285 
1286 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1287 {
1288  DynBuffer *d = opaque;
1289 
1290  if (whence == SEEK_CUR)
1291  offset += d->pos;
1292  else if (whence == SEEK_END)
1293  offset += d->size;
1294  if (offset < 0 || offset > 0x7fffffffLL)
1295  return -1;
1296  d->pos = offset;
1297  return 0;
1298 }
1299 
1300 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1301 {
1302  DynBuffer *d;
1303  unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1304 
1305  if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1306  return -1;
1307  d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1308  if (!d)
1309  return AVERROR(ENOMEM);
1310  d->io_buffer_size = io_buffer_size;
1311  *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1312  max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1313  max_packet_size ? NULL : dyn_buf_seek);
1314  if(!*s) {
1315  av_free(d);
1316  return AVERROR(ENOMEM);
1317  }
1318  (*s)->max_packet_size = max_packet_size;
1319  return 0;
1320 }
1321 
1323 {
1324  return url_open_dyn_buf_internal(s, 0);
1325 }
1326 
1327 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1328 {
1329  if (max_packet_size <= 0)
1330  return -1;
1331  return url_open_dyn_buf_internal(s, max_packet_size);
1332 }
1333 
1335 {
1336  DynBuffer *d;
1337 
1338  if (!s) {
1339  *pbuffer = NULL;
1340  return 0;
1341  }
1342 
1343  avio_flush(s);
1344 
1345  d = s->opaque;
1346  *pbuffer = d->buffer;
1347 
1348  return d->size;
1349 }
1350 
1352 {
1353  DynBuffer *d;
1354  int size;
1355  static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
1356  int padding = 0;
1357 
1358  if (!s) {
1359  *pbuffer = NULL;
1360  return 0;
1361  }
1362 
1363  /* don't attempt to pad fixed-size packet buffers */
1364  if (!s->max_packet_size) {
1365  avio_write(s, padbuf, sizeof(padbuf));
1366  padding = AV_INPUT_BUFFER_PADDING_SIZE;
1367  }
1368 
1369  avio_flush(s);
1370 
1371  d = s->opaque;
1372  *pbuffer = d->buffer;
1373  size = d->size;
1374  av_free(d);
1375 
1376  avio_context_free(&s);
1377 
1378  return size - padding;
1379 }
1380 
1382 {
1383  uint8_t *tmp;
1384  if (!*s)
1385  return;
1386  avio_close_dyn_buf(*s, &tmp);
1387  av_free(tmp);
1388  *s = NULL;
1389 }
1390 
1391 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1392 {
1393  DynBuffer *d = opaque;
1394 
1395  d->pos += buf_size;
1396  if (d->pos > d->size)
1397  d->size = d->pos;
1398  return buf_size;
1399 }
1400 
1402 {
1403  int ret = url_open_dyn_buf_internal(s, 0);
1404  if (ret >= 0) {
1405  AVIOContext *pb = *s;
1407  }
1408  return ret;
1409 }
1410 
1412 {
1413  DynBuffer *d = s->opaque;
1414  int size;
1415 
1416  avio_flush(s);
1417 
1418  size = d->size;
1419  av_free(d);
1420 
1421  avio_context_free(&s);
1422 
1423  return size;
1424 }
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:671
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:455
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
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:129
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
Definition: aviobuf.c:1286
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:866
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:256
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1322
#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:361
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:307
#define av_realloc_f(p, o, n)
uint8_t io_buffer[1]
Definition: aviobuf.c:1235
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:473
AVOption.
Definition: opt.h:246
static int64_t io_read_seek(void *opaque, int stream_index, int64_t timestamp, int flags)
Definition: aviobuf.c:917
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int(* short_seek_get)(void *opaque)
A callback that is used instead of short_seek_threshold.
Definition: avio.h:339
const char * fmt
Definition: avisynth_c.h:769
int64_t last_time
Definition: avio.h:333
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:228
int writeout_count
writeout statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:292
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:229
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:421
#define D
Definition: aviobuf.c:63
int write_flag
true if open for writing
Definition: avio.h:241
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:45
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1334
#define AVIO_SEEKABLE_TIME
Seeking by timestamp with avio_seek_time() is possible.
Definition: avio.h:45
#define vsnprintf
Definition: snprintf.h:36
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:202
const char * b
Definition: vf_curves.c:113
#define AVIO_FLAG_READ
read-only
Definition: avio.h:655
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:193
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:760
int min_packet_size
Try to buffer at least this amount of data before flushing it.
Definition: avio.h:352
int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
Read contents of h into print buffer, up to max_size bytes, or up to EOF.
Definition: aviobuf.c:1192
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:656
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
#define OFFSET(x)
Definition: aviobuf.c:61
void avio_wl24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:479
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:194
int flags
Definition: url.h:43
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:1146
int64_t(* seek)(void *opaque, int64_t offset, int whence)
Definition: avio.h:237
void * opaque
A private pointer, passed to the read/write/seek/...
Definition: avio.h:233
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:375
#define IO_BUFFER_SIZE
Definition: aviobuf.c:36
static const AVClass * ff_avio_child_class_next(const AVClass *prev)
Definition: aviobuf.c:56
static int io_read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:885
Trailer data, which doesn't contain actual content, but only for finalizing the output file...
Definition: avio.h:140
Format I/O context.
Definition: avformat.h:1325
int avio_accept(AVIOContext *s, AVIOContext **c)
Accept and allocate a client context on a server context.
Definition: aviobuf.c:1210
URLContext * h
Definition: aviobuf.c:46
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:383
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:367
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:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
uint8_t
#define av_malloc(s)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVOptions.
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition: avio.h:146
static void fill_buffer(AVIOContext *s)
Definition: aviobuf.c:547
static int io_read_pause(void *opaque, int pause)
Definition: aviobuf.c:909
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:617
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:491
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:236
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1401
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:81
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:128
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:158
Public header for CRC hash function implementation.
int64_t bytes_read
Bytes read statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:280
char * protocol_whitelist
',' separated list of allowed protocols.
Definition: avformat.h:1863
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
int ffio_set_buf_size(AVIOContext *s, int buf_size)
Definition: aviobuf.c:1012
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:703
ptrdiff_t size
Definition: opengl_enc.c:101
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:485
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:467
int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist)
Definition: aviobuf.c:1090
unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:605
const OptionDef options[]
Definition: ffserver.c:3948
#define av_log(a,...)
static void flush_buffer(AVIOContext *s)
Definition: aviobuf.c:178
int max_packet_size
Definition: avio.h:242
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
int64_t written
Definition: avio.h:341
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int io_buffer_size
Definition: aviobuf.c:1234
int(* write_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:236
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:792
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:464
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1271
const char * protocol_whitelist
Definition: url.h:49
int(* read_packet)(void *opaque, uint8_t *buf, int buf_size)
Definition: avio.h:235
static const AVOption ff_avio_options[]
Definition: aviobuf.c:64
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
simple assert() macros that are a bit more flexible than ISO C assert().
unsigned long checksum
Definition: avio.h:243
static void * ff_avio_child_next(void *obj, void *prev)
Definition: aviobuf.c:49
#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:43
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
int direct
avio_read and avio_write should if possible be satisfied directly instead of going through a buffer...
Definition: avio.h:274
#define fail()
Definition: checkasm.h:109
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:752
static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
Definition: aviobuf.c:527
int seek_count
seek statistic This field is internal to libavformat and access from outside is not allowed...
Definition: avio.h:286
int avio_pause(AVIOContext *s, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e.g.
Definition: aviobuf.c:1166
int(* write_data_type)(void *opaque, uint8_t *buf, int buf_size, enum AVIODataMarkerType type, int64_t time)
A callback that is used instead of write_packet.
Definition: avio.h:320
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:768
This is any, unlabelled data.
Definition: avio.h:135
int size
Definition: aviobuf.c:1232
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:695
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int ffurl_get_short_seek(URLContext *h)
Return the current short seek threshold value for this URL.
Definition: avio.c:648
#define FFMIN(a, b)
Definition: common.h:96
const AVClass ff_avio_class
Definition: aviobuf.c:69
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1351
unsigned char * checksum_ptr
Definition: avio.h:244
int avio_printf(AVIOContext *s, const char *fmt,...)
Definition: aviobuf.c:1153
AVIODataMarkerType
Different data types that can be returned via the AVIO write_data_type callback.
Definition: avio.h:111
int ffio_open2_wrapper(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Definition: aviobuf.c:1115
int allocated_size
Definition: aviobuf.c:1232
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: avio.c:234
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:366
void avio_context_free(AVIOContext **ps)
Free the supplied IO context and everything associated with it.
Definition: aviobuf.c:146
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1238
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:807
#define GET_STR16(type, read)
Definition: aviobuf.c:842
static volatile int checksum
Definition: adler32.c:30
unsigned long(* update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size)
Definition: avio.h:245
int ffurl_accept(URLContext *s, URLContext **c)
Accept an URLContext c on an URLContext s.
Definition: avio.c:226
int buffer_size
Maximum buffer size.
Definition: avio.h:227
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:461
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:646
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
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:1327
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
const char * protocol_blacklist
Definition: url.h:50
#define PUT_STR16(type, big_endian)
Definition: aviobuf.c:424
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:1085
static int io_short_seek(void *opaque)
Definition: aviobuf.c:903
int64_t(* url_read_seek)(URLContext *h, int stream_index, int64_t timestamp, int flags)
Definition: url.h:83
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file...
Definition: aviobuf.c:1042
static int url_resetbuf(AVIOContext *s, int flags)
Definition: aviobuf.c:1028
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:776
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1121
void * buf
Definition: avisynth_c.h:690
Definition: url.h:38
GLint GLenum type
Definition: opengl_enc.c:105
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:216
#define AVSEEK_FORCE
Passing this flag as the "whence" parameter to a seek function causes it to seek by any means (like r...
Definition: avio.h:540
#define AVIO_FLAG_DIRECT
Use direct mode.
Definition: avio.h:682
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:784
int pos
Definition: aviobuf.c:1232
int short_seek_threshold
Threshold to favor readahead over seek.
Definition: avio.h:305
#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:448
int ignore_boundary_point
If set, don't call write_data_type separately for AVIO_DATA_MARKER_BOUNDARY_POINT, but ignore them and treat them as AVIO_DATA_MARKER_UNKNOWN (to avoid needlessly small chunks of data returned from the callback).
Definition: avio.h:327
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1381
int error
contains the error code or 0 if no error happened
Definition: avio.h:246
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:983
int(* read_pause)(void *opaque, int pause)
Pause or resume playback for network streaming protocols - e.g.
Definition: avio.h:250
int ff_get_v_length(uint64_t val)
Get the length in bytes which is needed to store val as v.
Definition: aviobuf.c:435
int ffurl_close(URLContext *h)
Definition: avio.c:467
const AVClass ffurl_context_class
Definition: avio.c:63
int orig_buffer_size
Original buffer size used internally after probing and ensure seekback to reset the buffer size This ...
Definition: avio.h:299
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:349
enum AVIODataMarkerType current_type
Internal, not meant to be used from outside of AVIOContext.
Definition: avio.h:332
int avio_handshake(AVIOContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: aviobuf.c:1222
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:1109
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:334
Main libavformat public API header.
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1544
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:824
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:434
const struct URLProtocol * prot
Definition: url.h:40
int ffio_fdopen(AVIOContext **s, URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h...
Definition: aviobuf.c:925
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:625
static double c[64]
int64_t pos
position in the file of the current buffer
Definition: avio.h:238
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:532
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:769
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition: aviobuf.c:874
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:799
unsigned char * buf_ptr_max
Maximum reached position before a backward seek in the write buffer, used keeping track of already wr...
Definition: avio.h:347
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:122
#define av_free(p)
uint8_t * buffer
Definition: aviobuf.c:1233
static int put_str16(AVIOContext *s, const char *str, const int be)
Definition: aviobuf.c:394
int eof_reached
true if eof reached
Definition: avio.h:240
static int io_write_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:891
void ff_put_v(AVIOContext *bc, uint64_t val)
Put val using a variable number of bytes.
Definition: aviobuf.c:445
int len
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1411
static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
Definition: aviobuf.c:1391
static void writeout(AVIOContext *s, const uint8_t *data, int len)
Definition: aviobuf.c:151
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:715
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
Definition: aviobuf.c:1300
char * protocol_blacklist
',' separated list of disallowed protocols.
Definition: avformat.h:1898
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition: url.h:44
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
#define av_freep(p)
unbuffered private I/O API
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:599
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:637
int min_packet_size
if non zero, the stream is packetized with this min packet size
Definition: url.h:51
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:407
static int64_t io_seek(void *opaque, int64_t offset, int whence)
Definition: aviobuf.c:897
Header data; this needs to be present for the stream to be decodeable.
Definition: avio.h:115
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
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:1173
unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:611
GLuint buffer
Definition: opengl_enc.c:102
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
static uint8_t tmp[11]
Definition: aes_ctr.c:26