]> git.sesse.net Git - ffmpeg/blob - libavformat/aviobuf.c
hwcontext_vulkan: remove plane size alignment checks when host importing
[ffmpeg] / libavformat / aviobuf.c
1 /*
2  * buffered I/O
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/bprint.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/log.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
29 #include "avformat.h"
30 #include "avio.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33 #include "url.h"
34 #include <stdarg.h>
35
36 #define IO_BUFFER_SIZE 32768
37
38 /**
39  * Do seeks within this distance ahead of the current buffer by skipping
40  * data instead of calling the protocol seek function, for seekable
41  * protocols.
42  */
43 #define SHORT_SEEK_THRESHOLD 4096
44
45 static void *ff_avio_child_next(void *obj, void *prev)
46 {
47     AVIOContext *s = obj;
48     return prev ? NULL : s->opaque;
49 }
50
51 #if FF_API_CHILD_CLASS_NEXT
52 static const AVClass *ff_avio_child_class_next(const AVClass *prev)
53 {
54     return prev ? NULL : &ffurl_context_class;
55 }
56 #endif
57
58 static const AVClass *child_class_iterate(void **iter)
59 {
60     const AVClass *c = *iter ? NULL : &ffurl_context_class;
61     *iter = (void*)(uintptr_t)c;
62     return c;
63 }
64
65 #define OFFSET(x) offsetof(AVIOContext,x)
66 #define E AV_OPT_FLAG_ENCODING_PARAM
67 #define D AV_OPT_FLAG_DECODING_PARAM
68 static const AVOption ff_avio_options[] = {
69     {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL },  0, 0, D },
70     { NULL },
71 };
72
73 const AVClass ff_avio_class = {
74     .class_name = "AVIOContext",
75     .item_name  = av_default_item_name,
76     .version    = LIBAVUTIL_VERSION_INT,
77     .option     = ff_avio_options,
78     .child_next = ff_avio_child_next,
79 #if FF_API_CHILD_CLASS_NEXT
80     .child_class_next = ff_avio_child_class_next,
81 #endif
82     .child_class_iterate = child_class_iterate,
83 };
84
85 static void fill_buffer(AVIOContext *s);
86 static int url_resetbuf(AVIOContext *s, int flags);
87
88 int ffio_init_context(AVIOContext *s,
89                   unsigned char *buffer,
90                   int buffer_size,
91                   int write_flag,
92                   void *opaque,
93                   int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
94                   int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
95                   int64_t (*seek)(void *opaque, int64_t offset, int whence))
96 {
97     memset(s, 0, sizeof(AVIOContext));
98
99     s->buffer      = buffer;
100     s->orig_buffer_size =
101     s->buffer_size = buffer_size;
102     s->buf_ptr     = buffer;
103     s->buf_ptr_max = buffer;
104     s->opaque      = opaque;
105     s->direct      = 0;
106
107     url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
108
109     s->write_packet    = write_packet;
110     s->read_packet     = read_packet;
111     s->seek            = seek;
112     s->pos             = 0;
113     s->eof_reached     = 0;
114     s->error           = 0;
115     s->seekable        = seek ? AVIO_SEEKABLE_NORMAL : 0;
116     s->min_packet_size = 0;
117     s->max_packet_size = 0;
118     s->update_checksum = NULL;
119     s->short_seek_threshold = SHORT_SEEK_THRESHOLD;
120
121     if (!read_packet && !write_flag) {
122         s->pos     = buffer_size;
123         s->buf_end = s->buffer + buffer_size;
124     }
125     s->read_pause = NULL;
126     s->read_seek  = NULL;
127
128     s->write_data_type       = NULL;
129     s->ignore_boundary_point = 0;
130     s->current_type          = AVIO_DATA_MARKER_UNKNOWN;
131     s->last_time             = AV_NOPTS_VALUE;
132     s->short_seek_get        = NULL;
133     s->written               = 0;
134
135     return 0;
136 }
137
138 AVIOContext *avio_alloc_context(
139                   unsigned char *buffer,
140                   int buffer_size,
141                   int write_flag,
142                   void *opaque,
143                   int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
144                   int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
145                   int64_t (*seek)(void *opaque, int64_t offset, int whence))
146 {
147     AVIOContext *s = av_malloc(sizeof(AVIOContext));
148     if (!s)
149         return NULL;
150     ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
151                   read_packet, write_packet, seek);
152     return s;
153 }
154
155 void avio_context_free(AVIOContext **ps)
156 {
157     av_freep(ps);
158 }
159
160 static void writeout(AVIOContext *s, const uint8_t *data, int len)
161 {
162     if (!s->error) {
163         int ret = 0;
164         if (s->write_data_type)
165             ret = s->write_data_type(s->opaque, (uint8_t *)data,
166                                      len,
167                                      s->current_type,
168                                      s->last_time);
169         else if (s->write_packet)
170             ret = s->write_packet(s->opaque, (uint8_t *)data, len);
171         if (ret < 0) {
172             s->error = ret;
173         } else {
174             if (s->pos + len > s->written)
175                 s->written = s->pos + len;
176         }
177     }
178     if (s->current_type == AVIO_DATA_MARKER_SYNC_POINT ||
179         s->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
180         s->current_type = AVIO_DATA_MARKER_UNKNOWN;
181     }
182     s->last_time = AV_NOPTS_VALUE;
183     s->writeout_count ++;
184     s->pos += len;
185 }
186
187 static void flush_buffer(AVIOContext *s)
188 {
189     s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
190     if (s->write_flag && s->buf_ptr_max > s->buffer) {
191         writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
192         if (s->update_checksum) {
193             s->checksum     = s->update_checksum(s->checksum, s->checksum_ptr,
194                                                  s->buf_ptr_max - s->checksum_ptr);
195             s->checksum_ptr = s->buffer;
196         }
197     }
198     s->buf_ptr = s->buf_ptr_max = s->buffer;
199     if (!s->write_flag)
200         s->buf_end = s->buffer;
201 }
202
203 void avio_w8(AVIOContext *s, int b)
204 {
205     av_assert2(b>=-128 && b<=255);
206     *s->buf_ptr++ = b;
207     if (s->buf_ptr >= s->buf_end)
208         flush_buffer(s);
209 }
210
211 void ffio_fill(AVIOContext *s, int b, int count)
212 {
213     while (count > 0) {
214         int len = FFMIN(s->buf_end - s->buf_ptr, count);
215         memset(s->buf_ptr, b, len);
216         s->buf_ptr += len;
217
218         if (s->buf_ptr >= s->buf_end)
219             flush_buffer(s);
220
221         count -= len;
222     }
223 }
224
225 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
226 {
227     if (s->direct && !s->update_checksum) {
228         avio_flush(s);
229         writeout(s, buf, size);
230         return;
231     }
232     while (size > 0) {
233         int len = FFMIN(s->buf_end - s->buf_ptr, size);
234         memcpy(s->buf_ptr, buf, len);
235         s->buf_ptr += len;
236
237         if (s->buf_ptr >= s->buf_end)
238             flush_buffer(s);
239
240         buf += len;
241         size -= len;
242     }
243 }
244
245 void avio_flush(AVIOContext *s)
246 {
247     int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;
248     flush_buffer(s);
249     if (seekback)
250         avio_seek(s, seekback, SEEK_CUR);
251 }
252
253 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
254 {
255     int64_t offset1;
256     int64_t pos;
257     int force = whence & AVSEEK_FORCE;
258     int buffer_size;
259     int short_seek;
260     whence &= ~AVSEEK_FORCE;
261
262     if(!s)
263         return AVERROR(EINVAL);
264
265     if ((whence & AVSEEK_SIZE))
266         return s->seek ? s->seek(s->opaque, offset, AVSEEK_SIZE) : AVERROR(ENOSYS);
267
268     buffer_size = s->buf_end - s->buffer;
269     // pos is the absolute position that the beginning of s->buffer corresponds to in the file
270     pos = s->pos - (s->write_flag ? 0 : buffer_size);
271
272     if (whence != SEEK_CUR && whence != SEEK_SET)
273         return AVERROR(EINVAL);
274
275     if (whence == SEEK_CUR) {
276         offset1 = pos + (s->buf_ptr - s->buffer);
277         if (offset == 0)
278             return offset1;
279         if (offset > INT64_MAX - offset1)
280             return AVERROR(EINVAL);
281         offset += offset1;
282     }
283     if (offset < 0)
284         return AVERROR(EINVAL);
285
286     if (s->short_seek_get) {
287         short_seek = s->short_seek_get(s->opaque);
288         /* fallback to default short seek */
289         if (short_seek <= 0)
290             short_seek = s->short_seek_threshold;
291     } else
292         short_seek = s->short_seek_threshold;
293
294     offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
295     s->buf_ptr_max = FFMAX(s->buf_ptr_max, s->buf_ptr);
296     if ((!s->direct || !s->seek) &&
297         offset1 >= 0 && offset1 <= (s->write_flag ? s->buf_ptr_max - s->buffer : buffer_size)) {
298         /* can do the seek inside the buffer */
299         s->buf_ptr = s->buffer + offset1;
300     } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
301                offset1 <= buffer_size + short_seek) &&
302                !s->write_flag && offset1 >= 0 &&
303                (!s->direct || !s->seek) &&
304               (whence != SEEK_END || force)) {
305         while(s->pos < offset && !s->eof_reached)
306             fill_buffer(s);
307         if (s->eof_reached)
308             return AVERROR_EOF;
309         s->buf_ptr = s->buf_end - (s->pos - offset);
310     } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
311         int64_t res;
312
313         pos -= FFMIN(buffer_size>>1, pos);
314         if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
315             return res;
316         s->buf_end =
317         s->buf_ptr = s->buffer;
318         s->pos = pos;
319         s->eof_reached = 0;
320         fill_buffer(s);
321         return avio_seek(s, offset, SEEK_SET | force);
322     } else {
323         int64_t res;
324         if (s->write_flag) {
325             flush_buffer(s);
326         }
327         if (!s->seek)
328             return AVERROR(EPIPE);
329         if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
330             return res;
331         s->seek_count ++;
332         if (!s->write_flag)
333             s->buf_end = s->buffer;
334         s->buf_ptr = s->buf_ptr_max = s->buffer;
335         s->pos = offset;
336     }
337     s->eof_reached = 0;
338     return offset;
339 }
340
341 int64_t avio_skip(AVIOContext *s, int64_t offset)
342 {
343     return avio_seek(s, offset, SEEK_CUR);
344 }
345
346 int64_t avio_size(AVIOContext *s)
347 {
348     int64_t size;
349
350     if (!s)
351         return AVERROR(EINVAL);
352
353     if (s->written)
354         return s->written;
355
356     if (!s->seek)
357         return AVERROR(ENOSYS);
358     size = s->seek(s->opaque, 0, AVSEEK_SIZE);
359     if (size < 0) {
360         if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
361             return size;
362         size++;
363         s->seek(s->opaque, s->pos, SEEK_SET);
364     }
365     return size;
366 }
367
368 int avio_feof(AVIOContext *s)
369 {
370     if(!s)
371         return 0;
372     if(s->eof_reached){
373         s->eof_reached=0;
374         fill_buffer(s);
375     }
376     return s->eof_reached;
377 }
378
379 void avio_wl32(AVIOContext *s, unsigned int val)
380 {
381     avio_w8(s, (uint8_t) val       );
382     avio_w8(s, (uint8_t)(val >> 8 ));
383     avio_w8(s, (uint8_t)(val >> 16));
384     avio_w8(s,           val >> 24 );
385 }
386
387 void avio_wb32(AVIOContext *s, unsigned int val)
388 {
389     avio_w8(s,           val >> 24 );
390     avio_w8(s, (uint8_t)(val >> 16));
391     avio_w8(s, (uint8_t)(val >> 8 ));
392     avio_w8(s, (uint8_t) val       );
393 }
394
395 int avio_put_str(AVIOContext *s, const char *str)
396 {
397     int len = 1;
398     if (str) {
399         len += strlen(str);
400         avio_write(s, (const unsigned char *) str, len);
401     } else
402         avio_w8(s, 0);
403     return len;
404 }
405
406 static inline int put_str16(AVIOContext *s, const char *str, const int be)
407 {
408     const uint8_t *q = str;
409     int ret = 0;
410     int err = 0;
411
412     while (*q) {
413         uint32_t ch;
414         uint16_t tmp;
415
416         GET_UTF8(ch, *q++, goto invalid;)
417         PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
418                   ret += 2;)
419         continue;
420 invalid:
421         av_log(s, AV_LOG_ERROR, "Invalid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
422         err = AVERROR(EINVAL);
423         if (!*(q-1))
424             break;
425     }
426     if (be)
427         avio_wb16(s, 0);
428     else
429         avio_wl16(s, 0);
430     if (err)
431         return err;
432     ret += 2;
433     return ret;
434 }
435
436 #define PUT_STR16(type, big_endian)                          \
437 int avio_put_str16 ## type(AVIOContext *s, const char *str)  \
438 {                                                            \
439 return put_str16(s, str, big_endian);                        \
440 }
441
442 PUT_STR16(le, 0)
443 PUT_STR16(be, 1)
444
445 #undef PUT_STR16
446
447 void avio_wl64(AVIOContext *s, uint64_t val)
448 {
449     avio_wl32(s, (uint32_t)(val & 0xffffffff));
450     avio_wl32(s, (uint32_t)(val >> 32));
451 }
452
453 void avio_wb64(AVIOContext *s, uint64_t val)
454 {
455     avio_wb32(s, (uint32_t)(val >> 32));
456     avio_wb32(s, (uint32_t)(val & 0xffffffff));
457 }
458
459 void avio_wl16(AVIOContext *s, unsigned int val)
460 {
461     avio_w8(s, (uint8_t)val);
462     avio_w8(s, (int)val >> 8);
463 }
464
465 void avio_wb16(AVIOContext *s, unsigned int val)
466 {
467     avio_w8(s, (int)val >> 8);
468     avio_w8(s, (uint8_t)val);
469 }
470
471 void avio_wl24(AVIOContext *s, unsigned int val)
472 {
473     avio_wl16(s, val & 0xffff);
474     avio_w8(s, (int)val >> 16);
475 }
476
477 void avio_wb24(AVIOContext *s, unsigned int val)
478 {
479     avio_wb16(s, (int)val >> 8);
480     avio_w8(s, (uint8_t)val);
481 }
482
483 void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
484 {
485     if (type == AVIO_DATA_MARKER_FLUSH_POINT) {
486         if (s->buf_ptr - s->buffer >= s->min_packet_size)
487             avio_flush(s);
488         return;
489     }
490     if (!s->write_data_type)
491         return;
492     // If ignoring boundary points, just treat it as unknown
493     if (type == AVIO_DATA_MARKER_BOUNDARY_POINT && s->ignore_boundary_point)
494         type = AVIO_DATA_MARKER_UNKNOWN;
495     // Avoid unnecessary flushes if we are already in non-header/trailer
496     // data and setting the type to unknown
497     if (type == AVIO_DATA_MARKER_UNKNOWN &&
498         (s->current_type != AVIO_DATA_MARKER_HEADER &&
499          s->current_type != AVIO_DATA_MARKER_TRAILER))
500         return;
501
502     switch (type) {
503     case AVIO_DATA_MARKER_HEADER:
504     case AVIO_DATA_MARKER_TRAILER:
505         // For header/trailer, ignore a new marker of the same type;
506         // consecutive header/trailer markers can be merged.
507         if (type == s->current_type)
508             return;
509         break;
510     }
511
512     // If we've reached here, we have a new, noteworthy marker.
513     // Flush the previous data and mark the start of the new data.
514     avio_flush(s);
515     s->current_type = type;
516     s->last_time = time;
517 }
518
519 static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
520 {
521     int ret;
522
523     if (!s->read_packet)
524         return AVERROR(EINVAL);
525     ret = s->read_packet(s->opaque, buf, size);
526 #if FF_API_OLD_AVIO_EOF_0
527     if (!ret && !s->max_packet_size) {
528         av_log(NULL, AV_LOG_WARNING, "Invalid return value 0 for stream protocol\n");
529         ret = AVERROR_EOF;
530     }
531 #else
532     av_assert2(ret || s->max_packet_size);
533 #endif
534     return ret;
535 }
536
537 /* Input stream */
538
539 static void fill_buffer(AVIOContext *s)
540 {
541     int max_buffer_size = s->max_packet_size ?
542                           s->max_packet_size : IO_BUFFER_SIZE;
543     uint8_t *dst        = s->buf_end - s->buffer + max_buffer_size < s->buffer_size ?
544                           s->buf_end : s->buffer;
545     int len             = s->buffer_size - (dst - s->buffer);
546
547     /* can't fill the buffer without read_packet, just set EOF if appropriate */
548     if (!s->read_packet && s->buf_ptr >= s->buf_end)
549         s->eof_reached = 1;
550
551     /* no need to do anything if EOF already reached */
552     if (s->eof_reached)
553         return;
554
555     if (s->update_checksum && dst == s->buffer) {
556         if (s->buf_end > s->checksum_ptr)
557             s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
558                                              s->buf_end - s->checksum_ptr);
559         s->checksum_ptr = s->buffer;
560     }
561
562     /* make buffer smaller in case it ended up large after probing */
563     if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size && len >= s->orig_buffer_size) {
564         if (dst == s->buffer && s->buf_ptr != dst) {
565             int ret = ffio_set_buf_size(s, s->orig_buffer_size);
566             if (ret < 0)
567                 av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
568
569             s->checksum_ptr = dst = s->buffer;
570         }
571         len = s->orig_buffer_size;
572     }
573
574     len = read_packet_wrapper(s, dst, len);
575     if (len == AVERROR_EOF) {
576         /* do not modify buffer if EOF reached so that a seek back can
577            be done without rereading data */
578         s->eof_reached = 1;
579     } else if (len < 0) {
580         s->eof_reached = 1;
581         s->error= len;
582     } else {
583         s->pos += len;
584         s->buf_ptr = dst;
585         s->buf_end = dst + len;
586         s->bytes_read += len;
587     }
588 }
589
590 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
591                                     unsigned int len)
592 {
593     return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
594 }
595
596 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
597                                     unsigned int len)
598 {
599     return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
600 }
601
602 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
603                                 unsigned int len)
604 {
605     return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
606 }
607
608 unsigned long ffio_get_checksum(AVIOContext *s)
609 {
610     s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
611                                      s->buf_ptr - s->checksum_ptr);
612     s->update_checksum = NULL;
613     return s->checksum;
614 }
615
616 void ffio_init_checksum(AVIOContext *s,
617                    unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
618                    unsigned long checksum)
619 {
620     s->update_checksum = update_checksum;
621     if (s->update_checksum) {
622         s->checksum     = checksum;
623         s->checksum_ptr = s->buf_ptr;
624     }
625 }
626
627 /* XXX: put an inline version */
628 int avio_r8(AVIOContext *s)
629 {
630     if (s->buf_ptr >= s->buf_end)
631         fill_buffer(s);
632     if (s->buf_ptr < s->buf_end)
633         return *s->buf_ptr++;
634     return 0;
635 }
636
637 int avio_read(AVIOContext *s, unsigned char *buf, int size)
638 {
639     int len, size1;
640
641     size1 = size;
642     while (size > 0) {
643         len = FFMIN(s->buf_end - s->buf_ptr, size);
644         if (len == 0 || s->write_flag) {
645             if((s->direct || size > s->buffer_size) && !s->update_checksum) {
646                 // bypass the buffer and read data directly into buf
647                 len = read_packet_wrapper(s, buf, size);
648                 if (len == AVERROR_EOF) {
649                     /* do not modify buffer if EOF reached so that a seek back can
650                     be done without rereading data */
651                     s->eof_reached = 1;
652                     break;
653                 } else if (len < 0) {
654                     s->eof_reached = 1;
655                     s->error= len;
656                     break;
657                 } else {
658                     s->pos += len;
659                     s->bytes_read += len;
660                     size -= len;
661                     buf += len;
662                     // reset the buffer
663                     s->buf_ptr = s->buffer;
664                     s->buf_end = s->buffer/* + len*/;
665                 }
666             } else {
667                 fill_buffer(s);
668                 len = s->buf_end - s->buf_ptr;
669                 if (len == 0)
670                     break;
671             }
672         } else {
673             memcpy(buf, s->buf_ptr, len);
674             buf += len;
675             s->buf_ptr += len;
676             size -= len;
677         }
678     }
679     if (size1 == size) {
680         if (s->error)      return s->error;
681         if (avio_feof(s))  return AVERROR_EOF;
682     }
683     return size1 - size;
684 }
685
686 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
687 {
688     int ret = avio_read(s, buf, size);
689     if (ret != size)
690         return AVERROR_INVALIDDATA;
691     return ret;
692 }
693
694 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
695 {
696     if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
697         *data = s->buf_ptr;
698         s->buf_ptr += size;
699         return size;
700     } else {
701         *data = buf;
702         return avio_read(s, buf, size);
703     }
704 }
705
706 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
707 {
708     int len;
709
710     if (size < 0)
711         return AVERROR(EINVAL);
712
713     if (s->read_packet && s->write_flag) {
714         len = read_packet_wrapper(s, buf, size);
715         if (len > 0)
716             s->pos += len;
717         return len;
718     }
719
720     len = s->buf_end - s->buf_ptr;
721     if (len == 0) {
722         /* Reset the buf_end pointer to the start of the buffer, to make sure
723          * the fill_buffer call tries to read as much data as fits into the
724          * full buffer, instead of just what space is left after buf_end.
725          * This avoids returning partial packets at the end of the buffer,
726          * for packet based inputs.
727          */
728         s->buf_end = s->buf_ptr = s->buffer;
729         fill_buffer(s);
730         len = s->buf_end - s->buf_ptr;
731     }
732     if (len > size)
733         len = size;
734     memcpy(buf, s->buf_ptr, len);
735     s->buf_ptr += len;
736     if (!len) {
737         if (s->error)      return s->error;
738         if (avio_feof(s))  return AVERROR_EOF;
739     }
740     return len;
741 }
742
743 unsigned int avio_rl16(AVIOContext *s)
744 {
745     unsigned int val;
746     val = avio_r8(s);
747     val |= avio_r8(s) << 8;
748     return val;
749 }
750
751 unsigned int avio_rl24(AVIOContext *s)
752 {
753     unsigned int val;
754     val = avio_rl16(s);
755     val |= avio_r8(s) << 16;
756     return val;
757 }
758
759 unsigned int avio_rl32(AVIOContext *s)
760 {
761     unsigned int val;
762     val = avio_rl16(s);
763     val |= avio_rl16(s) << 16;
764     return val;
765 }
766
767 uint64_t avio_rl64(AVIOContext *s)
768 {
769     uint64_t val;
770     val = (uint64_t)avio_rl32(s);
771     val |= (uint64_t)avio_rl32(s) << 32;
772     return val;
773 }
774
775 unsigned int avio_rb16(AVIOContext *s)
776 {
777     unsigned int val;
778     val = avio_r8(s) << 8;
779     val |= avio_r8(s);
780     return val;
781 }
782
783 unsigned int avio_rb24(AVIOContext *s)
784 {
785     unsigned int val;
786     val = avio_rb16(s) << 8;
787     val |= avio_r8(s);
788     return val;
789 }
790 unsigned int avio_rb32(AVIOContext *s)
791 {
792     unsigned int val;
793     val = avio_rb16(s) << 16;
794     val |= avio_rb16(s);
795     return val;
796 }
797
798 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
799 {
800     int i = 0;
801     char c;
802
803     do {
804         c = avio_r8(s);
805         if (c && i < maxlen-1)
806             buf[i++] = c;
807     } while (c != '\n' && c != '\r' && c);
808     if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
809         avio_skip(s, -1);
810
811     buf[i] = 0;
812     return i;
813 }
814
815 int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
816 {
817     int len = ff_get_line(s, buf, maxlen);
818     while (len > 0 && av_isspace(buf[len - 1]))
819         buf[--len] = '\0';
820     return len;
821 }
822
823 int64_t ff_read_line_to_bprint(AVIOContext *s, AVBPrint *bp)
824 {
825     int len, end;
826     int64_t read = 0;
827     char tmp[1024];
828     char c;
829
830     do {
831         len = 0;
832         do {
833             c = avio_r8(s);
834             end = (c == '\r' || c == '\n' || c == '\0');
835             if (!end)
836                 tmp[len++] = c;
837         } while (!end && len < sizeof(tmp));
838         av_bprint_append_data(bp, tmp, len);
839         read += len;
840     } while (!end);
841
842     if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
843         avio_skip(s, -1);
844
845     if (!c && s->error)
846         return s->error;
847
848     if (!c && !read && avio_feof(s))
849         return AVERROR_EOF;
850
851     return read;
852 }
853
854 int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp)
855 {
856     int64_t ret;
857
858     av_bprint_clear(bp);
859     ret = ff_read_line_to_bprint(s, bp);
860     if (ret < 0)
861         return ret;
862
863     if (!av_bprint_is_complete(bp))
864         return AVERROR(ENOMEM);
865
866     return bp->len;
867 }
868
869 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
870 {
871     int i;
872
873     if (buflen <= 0)
874         return AVERROR(EINVAL);
875     // reserve 1 byte for terminating 0
876     buflen = FFMIN(buflen - 1, maxlen);
877     for (i = 0; i < buflen; i++)
878         if (!(buf[i] = avio_r8(s)))
879             return i + 1;
880     buf[i] = 0;
881     for (; i < maxlen; i++)
882         if (!avio_r8(s))
883             return i + 1;
884     return maxlen;
885 }
886
887 #define GET_STR16(type, read) \
888     int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
889 {\
890     char* q = buf;\
891     int ret = 0;\
892     if (buflen <= 0) \
893         return AVERROR(EINVAL); \
894     while (ret + 1 < maxlen) {\
895         uint8_t tmp;\
896         uint32_t ch;\
897         GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
898         if (!ch)\
899             break;\
900         PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
901     }\
902     *q = 0;\
903     return ret;\
904 }\
905
906 GET_STR16(le, avio_rl16)
907 GET_STR16(be, avio_rb16)
908
909 #undef GET_STR16
910
911 uint64_t avio_rb64(AVIOContext *s)
912 {
913     uint64_t val;
914     val = (uint64_t)avio_rb32(s) << 32;
915     val |= (uint64_t)avio_rb32(s);
916     return val;
917 }
918
919 uint64_t ffio_read_varlen(AVIOContext *bc){
920     uint64_t val = 0;
921     int tmp;
922
923     do{
924         tmp = avio_r8(bc);
925         val= (val<<7) + (tmp&127);
926     }while(tmp&128);
927     return val;
928 }
929
930 int ffio_fdopen(AVIOContext **s, URLContext *h)
931 {
932     uint8_t *buffer = NULL;
933     int buffer_size, max_packet_size;
934
935     max_packet_size = h->max_packet_size;
936     if (max_packet_size) {
937         buffer_size = max_packet_size; /* no need to bufferize more than one packet */
938     } else {
939         buffer_size = IO_BUFFER_SIZE;
940     }
941     buffer = av_malloc(buffer_size);
942     if (!buffer)
943         return AVERROR(ENOMEM);
944
945     *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
946                             (int (*)(void *, uint8_t *, int))  ffurl_read,
947                             (int (*)(void *, uint8_t *, int))  ffurl_write,
948                             (int64_t (*)(void *, int64_t, int))ffurl_seek);
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 = (int (*)(void *, int))h->prot->url_read_pause;
969         (*s)->read_seek  =
970             (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
971
972         if (h->prot->url_read_seek)
973             (*s)->seekable |= AVIO_SEEKABLE_TIME;
974     }
975     (*s)->short_seek_get = (int (*)(void *))ffurl_get_short_seek;
976     (*s)->av_class = &ff_avio_class;
977     return 0;
978 fail:
979     av_freep(&buffer);
980     return AVERROR(ENOMEM);
981 }
982
983 URLContext* ffio_geturlcontext(AVIOContext *s)
984 {
985     if (!s)
986         return NULL;
987
988     if (s->opaque && s->read_packet == (int (*)(void *, uint8_t *, int))ffurl_read)
989         return s->opaque;
990     else
991         return NULL;
992 }
993
994 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
995 {
996     uint8_t *buffer;
997     int max_buffer_size = s->max_packet_size ?
998                           s->max_packet_size : IO_BUFFER_SIZE;
999     int filled = s->buf_end - s->buffer;
1000     ptrdiff_t checksum_ptr_offset = s->checksum_ptr ? s->checksum_ptr - s->buffer : -1;
1001
1002     buf_size += s->buf_ptr - s->buffer + max_buffer_size;
1003
1004     if (buf_size < filled || s->seekable || !s->read_packet)
1005         return 0;
1006     av_assert0(!s->write_flag);
1007
1008     buffer = av_malloc(buf_size);
1009     if (!buffer)
1010         return AVERROR(ENOMEM);
1011
1012     memcpy(buffer, s->buffer, filled);
1013     av_free(s->buffer);
1014     s->buf_ptr = buffer + (s->buf_ptr - s->buffer);
1015     s->buf_end = buffer + (s->buf_end - s->buffer);
1016     s->buffer = buffer;
1017     s->buffer_size = buf_size;
1018     if (checksum_ptr_offset >= 0)
1019         s->checksum_ptr = s->buffer + checksum_ptr_offset;
1020     return 0;
1021 }
1022
1023 int ffio_set_buf_size(AVIOContext *s, int buf_size)
1024 {
1025     uint8_t *buffer;
1026     buffer = av_malloc(buf_size);
1027     if (!buffer)
1028         return AVERROR(ENOMEM);
1029
1030     av_free(s->buffer);
1031     s->buffer = buffer;
1032     s->orig_buffer_size =
1033     s->buffer_size = buf_size;
1034     s->buf_ptr = s->buf_ptr_max = buffer;
1035     url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
1036     return 0;
1037 }
1038
1039 int ffio_realloc_buf(AVIOContext *s, int buf_size)
1040 {
1041     uint8_t *buffer;
1042     int data_size;
1043
1044     if (!s->buffer_size)
1045         return ffio_set_buf_size(s, buf_size);
1046
1047     if (buf_size <= s->buffer_size)
1048         return 0;
1049
1050     buffer = av_malloc(buf_size);
1051     if (!buffer)
1052         return AVERROR(ENOMEM);
1053
1054     data_size = s->write_flag ? (s->buf_ptr - s->buffer) : (s->buf_end - s->buf_ptr);
1055     if (data_size > 0)
1056         memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size);
1057     av_free(s->buffer);
1058     s->buffer = buffer;
1059     s->orig_buffer_size = buf_size;
1060     s->buffer_size = buf_size;
1061     s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
1062     if (s->write_flag)
1063         s->buf_ptr_max = s->buffer + data_size;
1064
1065     s->buf_end = s->write_flag ? (s->buffer + s->buffer_size) : (s->buf_ptr + data_size);
1066
1067     return 0;
1068 }
1069
1070 static int url_resetbuf(AVIOContext *s, int flags)
1071 {
1072     av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
1073
1074     if (flags & AVIO_FLAG_WRITE) {
1075         s->buf_end = s->buffer + s->buffer_size;
1076         s->write_flag = 1;
1077     } else {
1078         s->buf_end = s->buffer;
1079         s->write_flag = 0;
1080     }
1081     return 0;
1082 }
1083
1084 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
1085 {
1086     int64_t buffer_start;
1087     int buffer_size;
1088     int overlap, new_size, alloc_size;
1089     uint8_t *buf = *bufp;
1090
1091     if (s->write_flag) {
1092         av_freep(bufp);
1093         return AVERROR(EINVAL);
1094     }
1095
1096     buffer_size = s->buf_end - s->buffer;
1097
1098     /* the buffers must touch or overlap */
1099     if ((buffer_start = s->pos - buffer_size) > buf_size) {
1100         av_freep(bufp);
1101         return AVERROR(EINVAL);
1102     }
1103
1104     overlap = buf_size - buffer_start;
1105     new_size = buf_size + buffer_size - overlap;
1106
1107     alloc_size = FFMAX(s->buffer_size, new_size);
1108     if (alloc_size > buf_size)
1109         if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
1110             return AVERROR(ENOMEM);
1111
1112     if (new_size > buf_size) {
1113         memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
1114         buf_size = new_size;
1115     }
1116
1117     av_free(s->buffer);
1118     s->buf_ptr = s->buffer = buf;
1119     s->buffer_size = alloc_size;
1120     s->pos = buf_size;
1121     s->buf_end = s->buf_ptr + buf_size;
1122     s->eof_reached = 0;
1123
1124     return 0;
1125 }
1126
1127 int avio_open(AVIOContext **s, const char *filename, int flags)
1128 {
1129     return avio_open2(s, filename, flags, NULL, NULL);
1130 }
1131
1132 int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
1133                          const AVIOInterruptCB *int_cb, AVDictionary **options,
1134                          const char *whitelist, const char *blacklist
1135                         )
1136 {
1137     URLContext *h;
1138     int err;
1139
1140     *s = NULL;
1141
1142     err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
1143     if (err < 0)
1144         return err;
1145     err = ffio_fdopen(s, h);
1146     if (err < 0) {
1147         ffurl_close(h);
1148         return err;
1149     }
1150     return 0;
1151 }
1152
1153 int avio_open2(AVIOContext **s, const char *filename, int flags,
1154                const AVIOInterruptCB *int_cb, AVDictionary **options)
1155 {
1156     return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
1157 }
1158
1159 int avio_close(AVIOContext *s)
1160 {
1161     URLContext *h;
1162
1163     if (!s)
1164         return 0;
1165
1166     avio_flush(s);
1167     h         = s->opaque;
1168     s->opaque = NULL;
1169
1170     av_freep(&s->buffer);
1171     if (s->write_flag)
1172         av_log(s, AV_LOG_VERBOSE, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
1173     else
1174         av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
1175     av_opt_free(s);
1176
1177     avio_context_free(&s);
1178
1179     return ffurl_close(h);
1180 }
1181
1182 int avio_closep(AVIOContext **s)
1183 {
1184     int ret = avio_close(*s);
1185     *s = NULL;
1186     return ret;
1187 }
1188
1189 int avio_printf(AVIOContext *s, const char *fmt, ...)
1190 {
1191     va_list ap;
1192     AVBPrint bp;
1193
1194     av_bprint_init(&bp, 0, INT_MAX);
1195     va_start(ap, fmt);
1196     av_vbprintf(&bp, fmt, ap);
1197     va_end(ap);
1198     if (!av_bprint_is_complete(&bp)) {
1199         av_bprint_finalize(&bp, NULL);
1200         s->error = AVERROR(ENOMEM);
1201         return AVERROR(ENOMEM);
1202     }
1203     avio_write(s, bp.str, bp.len);
1204     av_bprint_finalize(&bp, NULL);
1205     return bp.len;
1206 }
1207
1208 void avio_print_string_array(AVIOContext *s, const char *strings[])
1209 {
1210     for(; *strings; strings++)
1211         avio_write(s, (const unsigned char *)*strings, strlen(*strings));
1212 }
1213
1214 int avio_pause(AVIOContext *s, int pause)
1215 {
1216     if (!s->read_pause)
1217         return AVERROR(ENOSYS);
1218     return s->read_pause(s->opaque, pause);
1219 }
1220
1221 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1222                        int64_t timestamp, int flags)
1223 {
1224     int64_t ret;
1225     if (!s->read_seek)
1226         return AVERROR(ENOSYS);
1227     ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1228     if (ret >= 0) {
1229         int64_t pos;
1230         s->buf_ptr = s->buf_end; // Flush buffer
1231         pos = s->seek(s->opaque, 0, SEEK_CUR);
1232         if (pos >= 0)
1233             s->pos = pos;
1234         else if (pos != AVERROR(ENOSYS))
1235             ret = pos;
1236     }
1237     return ret;
1238 }
1239
1240 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1241 {
1242     int ret;
1243     char buf[1024];
1244     while (max_size) {
1245         ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1246         if (ret == AVERROR_EOF)
1247             return 0;
1248         if (ret <= 0)
1249             return ret;
1250         av_bprint_append_data(pb, buf, ret);
1251         if (!av_bprint_is_complete(pb))
1252             return AVERROR(ENOMEM);
1253         max_size -= ret;
1254     }
1255     return 0;
1256 }
1257
1258 int avio_accept(AVIOContext *s, AVIOContext **c)
1259 {
1260     int ret;
1261     URLContext *sc = s->opaque;
1262     URLContext *cc = NULL;
1263     ret = ffurl_accept(sc, &cc);
1264     if (ret < 0)
1265         return ret;
1266     return ffio_fdopen(c, cc);
1267 }
1268
1269 int avio_handshake(AVIOContext *c)
1270 {
1271     URLContext *cc = c->opaque;
1272     return ffurl_handshake(cc);
1273 }
1274
1275 /* output in a dynamic buffer */
1276
1277 typedef struct DynBuffer {
1278     int pos, size, allocated_size;
1279     uint8_t *buffer;
1280     int io_buffer_size;
1281     uint8_t io_buffer[1];
1282 } DynBuffer;
1283
1284 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1285 {
1286     DynBuffer *d = opaque;
1287     unsigned new_size;
1288
1289     /* reallocate buffer if needed */
1290     new_size = (unsigned)d->pos + buf_size;
1291     if (new_size < d->pos || new_size > INT_MAX)
1292         return AVERROR(ERANGE);
1293     if (new_size > d->allocated_size) {
1294         unsigned new_allocated_size = d->allocated_size ? d->allocated_size
1295                                                         : new_size;
1296         int err;
1297         while (new_size > new_allocated_size)
1298             new_allocated_size += new_allocated_size / 2 + 1;
1299
1300         new_allocated_size = FFMIN(new_allocated_size, INT_MAX);
1301
1302         if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1303             d->allocated_size = 0;
1304             d->size = 0;
1305             return err;
1306         }
1307         d->allocated_size = new_allocated_size;
1308     }
1309     memcpy(d->buffer + d->pos, buf, buf_size);
1310     d->pos = new_size;
1311     if (d->pos > d->size)
1312         d->size = d->pos;
1313     return buf_size;
1314 }
1315
1316 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1317 {
1318     unsigned char buf1[4];
1319     int ret;
1320
1321     /* packetized write: output the header */
1322     AV_WB32(buf1, buf_size);
1323     ret = dyn_buf_write(opaque, buf1, 4);
1324     if (ret < 0)
1325         return ret;
1326
1327     /* then the data */
1328     return dyn_buf_write(opaque, buf, buf_size);
1329 }
1330
1331 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1332 {
1333     DynBuffer *d = opaque;
1334
1335     if (whence == SEEK_CUR)
1336         offset += d->pos;
1337     else if (whence == SEEK_END)
1338         offset += d->size;
1339     if (offset < 0)
1340         return AVERROR(EINVAL);
1341     if (offset > INT_MAX)
1342         return AVERROR(ERANGE);
1343     d->pos = offset;
1344     return 0;
1345 }
1346
1347 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1348 {
1349     DynBuffer *d;
1350     unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1351
1352     if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1353         return AVERROR(ERANGE);
1354     d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1355     if (!d)
1356         return AVERROR(ENOMEM);
1357     d->io_buffer_size = io_buffer_size;
1358     *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1359                             max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1360                             max_packet_size ? NULL : dyn_buf_seek);
1361     if(!*s) {
1362         av_free(d);
1363         return AVERROR(ENOMEM);
1364     }
1365     (*s)->max_packet_size = max_packet_size;
1366     return 0;
1367 }
1368
1369 int avio_open_dyn_buf(AVIOContext **s)
1370 {
1371     return url_open_dyn_buf_internal(s, 0);
1372 }
1373
1374 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1375 {
1376     if (max_packet_size <= 0)
1377         return AVERROR(EINVAL);
1378     return url_open_dyn_buf_internal(s, max_packet_size);
1379 }
1380
1381 int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1382 {
1383     DynBuffer *d;
1384
1385     if (!s) {
1386         *pbuffer = NULL;
1387         return 0;
1388     }
1389     d = s->opaque;
1390
1391     if (!s->error && !d->size) {
1392         *pbuffer = d->io_buffer;
1393         return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer;
1394     }
1395
1396     avio_flush(s);
1397
1398     *pbuffer = d->buffer;
1399
1400     return d->size;
1401 }
1402
1403 void ffio_reset_dyn_buf(AVIOContext *s)
1404 {
1405     DynBuffer *d = s->opaque;
1406     int max_packet_size = s->max_packet_size;
1407
1408     ffio_init_context(s, d->io_buffer, d->io_buffer_size, 1, d, NULL,
1409                       s->write_packet, s->seek);
1410     s->max_packet_size = max_packet_size;
1411     d->pos = d->size = 0;
1412 }
1413
1414 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1415 {
1416     DynBuffer *d;
1417     int size;
1418     static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
1419     int padding = 0;
1420
1421     if (!s) {
1422         *pbuffer = NULL;
1423         return 0;
1424     }
1425
1426     /* don't attempt to pad fixed-size packet buffers */
1427     if (!s->max_packet_size) {
1428         avio_write(s, padbuf, sizeof(padbuf));
1429         padding = AV_INPUT_BUFFER_PADDING_SIZE;
1430     }
1431
1432     avio_flush(s);
1433
1434     d = s->opaque;
1435     *pbuffer = d->buffer;
1436     size = d->size;
1437     av_free(d);
1438
1439     avio_context_free(&s);
1440
1441     return size - padding;
1442 }
1443
1444 void ffio_free_dyn_buf(AVIOContext **s)
1445 {
1446     DynBuffer *d;
1447
1448     if (!*s)
1449         return;
1450
1451     d = (*s)->opaque;
1452     av_free(d->buffer);
1453     av_free(d);
1454     avio_context_free(s);
1455 }
1456
1457 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1458 {
1459     DynBuffer *d = opaque;
1460
1461     d->pos += buf_size;
1462     if (d->pos > d->size)
1463         d->size = d->pos;
1464     return buf_size;
1465 }
1466
1467 int ffio_open_null_buf(AVIOContext **s)
1468 {
1469     int ret = url_open_dyn_buf_internal(s, 0);
1470     if (ret >= 0) {
1471         AVIOContext *pb = *s;
1472         pb->write_packet = null_buf_write;
1473     }
1474     return ret;
1475 }
1476
1477 int ffio_close_null_buf(AVIOContext *s)
1478 {
1479     DynBuffer *d = s->opaque;
1480     int size;
1481
1482     avio_flush(s);
1483
1484     size = d->size;
1485     av_free(d);
1486
1487     avio_context_free(&s);
1488
1489     return size;
1490 }