avio: Keep track of the amount of data written
[libav.git] / libavformat / aviobuf.c
CommitLineData
de6d9b64 1/*
124e2884 2 * buffered I/O
19720f15 3 * Copyright (c) 2000,2001 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
245976da
DB
21
22#include "libavutil/crc.h"
1dee0aca 23#include "libavutil/dict.h"
aa805f94 24#include "libavutil/intreadwrite.h"
1dee0aca
MS
25#include "libavutil/log.h"
26#include "libavutil/opt.h"
de6d9b64 27#include "avformat.h"
5c91a675 28#include "avio.h"
e731b8d8 29#include "avio_internal.h"
603e5c0b 30#include "internal.h"
0589da0a 31#include "url.h"
abba3dd1 32#include <stdarg.h>
de6d9b64
FB
33
34#define IO_BUFFER_SIZE 32768
35
7939d0ed
MS
36/**
37 * Do seeks within this distance ahead of the current buffer by skipping
38 * data instead of calling the protocol seek function, for seekable
39 * protocols.
40 */
41#define SHORT_SEEK_THRESHOLD 4096
42
cae448cf 43typedef struct AVIOInternal {
ec4c4839
AK
44 const AVClass *class;
45
46 char *protocol_whitelist;
47 char *protocol_blacklist;
48
cae448cf 49 URLContext *h;
8c0ceafb 50 const URLProtocol **protocols;
cae448cf
AK
51} AVIOInternal;
52
ec4c4839
AK
53static void *io_priv_child_next(void *obj, void *prev)
54{
55 AVIOInternal *internal = obj;
56 return prev ? NULL : internal->h;
57}
58
59static const AVClass *io_priv_child_class_next(const AVClass *prev)
60{
61 return prev ? NULL : &ffurl_context_class;
62}
63
64#define OFFSET(x) offsetof(AVIOInternal, x)
65static const AVOption io_priv_options[] = {
66 { "protocol_whitelist", "A comma-separated list of allowed protocols",
67 OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING },
68 { "protocol_blacklist", "A comma-separated list of forbidden protocols",
69 OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING },
70 { NULL },
71};
72
73static const AVClass io_priv_class = {
74 .class_name = "AVIOContext",
75 .item_name = av_default_item_name,
76 .version = LIBAVUTIL_VERSION_INT,
77 .option = io_priv_options,
78 .child_next = io_priv_child_next,
79 .child_class_next = io_priv_child_class_next,
80};
81
c86d8aed 82static void *ff_avio_child_next(void *obj, void *prev)
1dee0aca
MS
83{
84 AVIOContext *s = obj;
ec4c4839 85 return prev ? NULL : s->opaque;
1dee0aca
MS
86}
87
c86d8aed 88static const AVClass *ff_avio_child_class_next(const AVClass *prev)
1dee0aca 89{
ec4c4839 90 return prev ? NULL : &io_priv_class;
1dee0aca
MS
91}
92
c86d8aed 93static const AVOption ff_avio_options[] = {
1dee0aca
MS
94 { NULL },
95};
96
c86d8aed 97const AVClass ff_avio_class = {
1dee0aca
MS
98 .class_name = "AVIOContext",
99 .item_name = av_default_item_name,
100 .version = LIBAVUTIL_VERSION_INT,
c86d8aed
MS
101 .option = ff_avio_options,
102 .child_next = ff_avio_child_next,
103 .child_class_next = ff_avio_child_class_next,
1dee0aca 104};
2439f2ca 105
ae628ec1 106static void fill_buffer(AVIOContext *s);
ae628ec1 107static int url_resetbuf(AVIOContext *s, int flags);
398f5d3f 108
e731b8d8 109int ffio_init_context(AVIOContext *s,
de6d9b64
FB
110 unsigned char *buffer,
111 int buffer_size,
112 int write_flag,
113 void *opaque,
0c1a9eda 114 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
576ae256 115 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
bc5c918e 116 int64_t (*seek)(void *opaque, int64_t offset, int whence))
de6d9b64 117{
1dd1c1c8 118 s->buffer = buffer;
de6d9b64 119 s->buffer_size = buffer_size;
1dd1c1c8
LB
120 s->buf_ptr = buffer;
121 s->opaque = opaque;
122
59d96941 123 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
1dd1c1c8
LB
124
125 s->write_packet = write_packet;
126 s->read_packet = read_packet;
127 s->seek = seek;
128 s->pos = 0;
129 s->must_flush = 0;
130 s->eof_reached = 0;
131 s->error = 0;
b5123601 132 s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
abba3dd1 133 s->max_packet_size = 0;
1dd1c1c8
LB
134 s->update_checksum = NULL;
135
136 if (!read_packet && !write_flag) {
137 s->pos = buffer_size;
942f3bb5
MN
138 s->buf_end = s->buffer + buffer_size;
139 }
e7e4810a
BA
140 s->read_pause = NULL;
141 s->read_seek = NULL;
1dd1c1c8 142
db7968bf
MS
143 s->write_data_type = NULL;
144 s->ignore_boundary_point = 0;
145 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
146 s->last_time = AV_NOPTS_VALUE;
147
de6d9b64
FB
148 return 0;
149}
115329f1 150
8d9ac969 151AVIOContext *avio_alloc_context(
1e0f3468
RD
152 unsigned char *buffer,
153 int buffer_size,
154 int write_flag,
155 void *opaque,
156 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
157 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
7f37f568
DB
158 int64_t (*seek)(void *opaque, int64_t offset, int whence))
159{
ae628ec1 160 AVIOContext *s = av_mallocz(sizeof(AVIOContext));
9e2dabed
CW
161 if (!s)
162 return NULL;
e731b8d8 163 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
1e0f3468
RD
164 read_packet, write_packet, seek);
165 return s;
166}
167
ae628ec1 168static void flush_buffer(AVIOContext *s)
de6d9b64
FB
169{
170 if (s->buf_ptr > s->buffer) {
3f75e511 171 int size = s->buf_ptr - s->buffer;
db7968bf
MS
172 if (!s->error) {
173 int ret = 0;
174 if (s->write_data_type)
175 ret = s->write_data_type(s->opaque, s->buffer,
3f75e511 176 size,
db7968bf
MS
177 s->current_type,
178 s->last_time);
179 else if (s->write_packet)
180 ret = s->write_packet(s->opaque, s->buffer,
3f75e511 181 size);
1dd1c1c8 182 if (ret < 0) {
576ae256 183 s->error = ret;
3f75e511
LB
184 } else {
185 if (s->pos + size > s->written)
186 s->written = s->pos + size;
576ae256
MN
187 }
188 }
db7968bf
MS
189 if (s->current_type == AVIO_DATA_MARKER_SYNC_POINT ||
190 s->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
191 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
192 }
193 s->last_time = AV_NOPTS_VALUE;
1dd1c1c8
LB
194 if (s->update_checksum) {
195 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
196 s->buf_ptr - s->checksum_ptr);
197 s->checksum_ptr = s->buffer;
ee9f36a8 198 }
3f75e511 199 s->pos += size;
de6d9b64
FB
200 }
201 s->buf_ptr = s->buffer;
202}
203
77eb5504 204void avio_w8(AVIOContext *s, int b)
de6d9b64 205{
3dc99a18 206 *s->buf_ptr++ = b;
115329f1 207 if (s->buf_ptr >= s->buf_end)
de6d9b64
FB
208 flush_buffer(s);
209}
210
0ac8e2bf 211void ffio_fill(AVIOContext *s, int b, int count)
17ee8f66
AH
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
77eb5504 225void avio_write(AVIOContext *s, const unsigned char *buf, int size)
de6d9b64 226{
de6d9b64 227 while (size > 0) {
90aa6ace 228 int len = FFMIN(s->buf_end - s->buf_ptr, size);
de6d9b64
FB
229 memcpy(s->buf_ptr, buf, len);
230 s->buf_ptr += len;
231
115329f1 232 if (s->buf_ptr >= s->buf_end)
de6d9b64
FB
233 flush_buffer(s);
234
235 buf += len;
236 size -= len;
237 }
238}
239
b7f2fdde 240void avio_flush(AVIOContext *s)
de6d9b64
FB
241{
242 flush_buffer(s);
243 s->must_flush = 0;
244}
245
6b4aa5da 246int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
de6d9b64 247{
bc5c918e
DB
248 int64_t offset1;
249 int64_t pos;
c6a5e087
TH
250 int force = whence & AVSEEK_FORCE;
251 whence &= ~AVSEEK_FORCE;
899681cd
BA
252
253 if(!s)
254 return AVERROR(EINVAL);
255
256 pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
de6d9b64
FB
257
258 if (whence != SEEK_CUR && whence != SEEK_SET)
8fa36ae0 259 return AVERROR(EINVAL);
115329f1 260
f40d1739
MN
261 if (whence == SEEK_CUR) {
262 offset1 = pos + (s->buf_ptr - s->buffer);
263 if (offset == 0)
264 return offset1;
265 offset += offset1;
266 }
267 offset1 = offset - pos;
268 if (!s->must_flush &&
3c18a7b1 269 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
f40d1739
MN
270 /* can do the seek inside the buffer */
271 s->buf_ptr = s->buffer + offset1;
83548fe8 272 } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
7939d0ed
MS
273 offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
274 !s->write_flag && offset1 >= 0 &&
7a6fe01f 275 (whence != SEEK_END || force)) {
398f5d3f
MN
276 while(s->pos < offset && !s->eof_reached)
277 fill_buffer(s);
5cb4b82a 278 if (s->eof_reached)
8d5e638f 279 return AVERROR_EOF;
398f5d3f 280 s->buf_ptr = s->buf_end + offset - s->pos;
f40d1739 281 } else {
9d30e068 282 int64_t res;
68fcdbf1 283
f40d1739 284 if (s->write_flag) {
de6d9b64
FB
285 flush_buffer(s);
286 s->must_flush = 1;
de6d9b64 287 }
9d30e068
SS
288 if (!s->seek)
289 return AVERROR(EPIPE);
290 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
68fcdbf1 291 return res;
39a127bf
BC
292 if (!s->write_flag)
293 s->buf_end = s->buffer;
294 s->buf_ptr = s->buffer;
f40d1739 295 s->pos = offset;
de6d9b64 296 }
f40d1739 297 s->eof_reached = 0;
de6d9b64
FB
298 return offset;
299}
300
76aa876e 301int64_t avio_size(AVIOContext *s)
a965c478 302{
bc5c918e 303 int64_t size;
115329f1 304
1dd1c1c8 305 if (!s)
899681cd
BA
306 return AVERROR(EINVAL);
307
3f75e511
LB
308 if (s->written)
309 return s->written;
310
a965c478 311 if (!s->seek)
4c4ef3db 312 return AVERROR(ENOSYS);
8e287af0 313 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
1dd1c1c8 314 if (size < 0) {
8f42f523
RB
315 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
316 return size;
317 size++;
1ae2c5f2 318 s->seek(s->opaque, s->pos, SEEK_SET);
8e287af0 319 }
a965c478
AJ
320 return size;
321}
322
77eb5504 323void avio_wl32(AVIOContext *s, unsigned int val)
de6d9b64 324{
77eb5504
AK
325 avio_w8(s, val);
326 avio_w8(s, val >> 8);
327 avio_w8(s, val >> 16);
328 avio_w8(s, val >> 24);
de6d9b64
FB
329}
330
77eb5504 331void avio_wb32(AVIOContext *s, unsigned int val)
de6d9b64 332{
77eb5504
AK
333 avio_w8(s, val >> 24);
334 avio_w8(s, val >> 16);
335 avio_w8(s, val >> 8);
336 avio_w8(s, val);
de6d9b64
FB
337}
338
ae628ec1 339int avio_put_str(AVIOContext *s, const char *str)
4efd5cf3
AK
340{
341 int len = 1;
342 if (str) {
343 len += strlen(str);
77eb5504 344 avio_write(s, (const unsigned char *) str, len);
4efd5cf3 345 } else
77eb5504 346 avio_w8(s, 0);
4efd5cf3 347 return len;
75bdb984
PG
348}
349
252d6200
LB
350#define PUT_STR16(type, write) \
351 int avio_put_str16 ## type(AVIOContext * s, const char *str) \
352 { \
353 const uint8_t *q = str; \
354 int ret = 0; \
355 \
356 while (*q) { \
357 uint32_t ch; \
358 uint16_t tmp; \
359 \
360 GET_UTF8(ch, *q++, break; ) \
361 PUT_UTF16(ch, tmp, write(s, tmp); ret += 2; ) \
362 } \
363 write(s, 0); \
364 ret += 2; \
365 return ret; \
366 }
dccbd97d 367
252d6200
LB
368PUT_STR16(le, avio_wl16)
369PUT_STR16(be, avio_wb16)
dccbd97d 370
252d6200 371#undef PUT_STR16
dccbd97d 372
1dd1c1c8
LB
373int ff_get_v_length(uint64_t val)
374{
375 int i = 1;
603e5c0b 376
1dd1c1c8 377 while (val >>= 7)
603e5c0b
MC
378 i++;
379
380 return i;
381}
382
1dd1c1c8
LB
383void ff_put_v(AVIOContext *bc, uint64_t val)
384{
385 int i = ff_get_v_length(val);
603e5c0b 386
1dd1c1c8
LB
387 while (--i > 0)
388 avio_w8(bc, 128 | (val >> (7 * i)));
603e5c0b 389
1dd1c1c8 390 avio_w8(bc, val & 127);
603e5c0b
MC
391}
392
77eb5504 393void avio_wl64(AVIOContext *s, uint64_t val)
de6d9b64 394{
77eb5504
AK
395 avio_wl32(s, (uint32_t)(val & 0xffffffff));
396 avio_wl32(s, (uint32_t)(val >> 32));
de6d9b64
FB
397}
398
77eb5504 399void avio_wb64(AVIOContext *s, uint64_t val)
de6d9b64 400{
77eb5504
AK
401 avio_wb32(s, (uint32_t)(val >> 32));
402 avio_wb32(s, (uint32_t)(val & 0xffffffff));
de6d9b64
FB
403}
404
77eb5504 405void avio_wl16(AVIOContext *s, unsigned int val)
de6d9b64 406{
77eb5504
AK
407 avio_w8(s, val);
408 avio_w8(s, val >> 8);
de6d9b64
FB
409}
410
77eb5504 411void avio_wb16(AVIOContext *s, unsigned int val)
de6d9b64 412{
77eb5504
AK
413 avio_w8(s, val >> 8);
414 avio_w8(s, val);
de6d9b64
FB
415}
416
77eb5504 417void avio_wl24(AVIOContext *s, unsigned int val)
ea395e8c 418{
77eb5504
AK
419 avio_wl16(s, val & 0xffff);
420 avio_w8(s, val >> 16);
ea395e8c
AJ
421}
422
77eb5504 423void avio_wb24(AVIOContext *s, unsigned int val)
a254c574 424{
77eb5504
AK
425 avio_wb16(s, val >> 8);
426 avio_w8(s, val);
a254c574
MN
427}
428
db7968bf
MS
429void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
430{
431 if (!s->write_data_type)
432 return;
433 // If ignoring boundary points, just treat it as unknown
434 if (type == AVIO_DATA_MARKER_BOUNDARY_POINT && s->ignore_boundary_point)
435 type = AVIO_DATA_MARKER_UNKNOWN;
436 // Avoid unnecessary flushes if we are already in non-header/trailer
437 // data and setting the type to unknown
438 if (type == AVIO_DATA_MARKER_UNKNOWN &&
439 (s->current_type != AVIO_DATA_MARKER_HEADER &&
440 s->current_type != AVIO_DATA_MARKER_TRAILER))
441 return;
442
443 switch (type) {
444 case AVIO_DATA_MARKER_HEADER:
445 case AVIO_DATA_MARKER_TRAILER:
446 // For header/trailer, ignore a new marker of the same type;
447 // consecutive header/trailer markers can be merged.
448 if (type == s->current_type)
449 return;
450 break;
451 }
452
453 // If we've reached here, we have a new, noteworthy marker.
454 // Flush the previous data and mark the start of the new data.
455 avio_flush(s);
456 s->current_type = type;
457 s->last_time = time;
458}
459
de6d9b64
FB
460/* Input stream */
461
ae628ec1 462static void fill_buffer(AVIOContext *s)
de6d9b64 463{
1dd1c1c8
LB
464 uint8_t *dst = !s->max_packet_size &&
465 s->buf_end - s->buffer < s->buffer_size ?
466 s->buf_end : s->buffer;
467 int len = s->buffer_size - (dst - s->buffer);
468 int max_buffer_size = s->max_packet_size ?
469 s->max_packet_size : IO_BUFFER_SIZE;
4589dc5c 470
511cf612 471 /* can't fill the buffer without read_packet, just set EOF if appropriate */
a2d1d216
JG
472 if (!s->read_packet && s->buf_ptr >= s->buf_end)
473 s->eof_reached = 1;
474
abba3dd1
FB
475 /* no need to do anything if EOF already reached */
476 if (s->eof_reached)
477 return;
ee9f36a8 478
1dd1c1c8
LB
479 if (s->update_checksum && dst == s->buffer) {
480 if (s->buf_end > s->checksum_ptr)
481 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
482 s->buf_end - s->checksum_ptr);
483 s->checksum_ptr = s->buffer;
ee9f36a8
MN
484 }
485
01d91b9b
TH
486 /* make buffer smaller in case it ended up large after probing */
487 if (s->buffer_size > max_buffer_size) {
59f65d95 488 ffio_set_buf_size(s, max_buffer_size);
01d91b9b
TH
489
490 s->checksum_ptr = dst = s->buffer;
491 len = s->buffer_size;
492 }
493
1dd1c1c8 494 if (s->read_packet)
c66f53cf 495 len = s->read_packet(s->opaque, dst, len);
4589dc5c
MN
496 else
497 len = 0;
abba3dd1
FB
498 if (len <= 0) {
499 /* do not modify buffer if EOF reached so that a seek back can
500 be done without rereading data */
de6d9b64 501 s->eof_reached = 1;
1dd1c1c8
LB
502 if (len < 0)
503 s->error = len;
abba3dd1
FB
504 } else {
505 s->pos += len;
4589dc5c
MN
506 s->buf_ptr = dst;
507 s->buf_end = dst + len;
de6d9b64
FB
508 }
509}
510
7f37f568
DB
511unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
512 unsigned int len)
513{
3abe5fbd 514 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
992e78f5
BC
515}
516
32d05934
AG
517unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
518 unsigned int len)
519{
520 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
521}
522
b64030f2 523unsigned long ffio_get_checksum(AVIOContext *s)
7f37f568 524{
1dd1c1c8
LB
525 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
526 s->buf_ptr - s->checksum_ptr);
527 s->update_checksum = NULL;
ee9f36a8
MN
528 return s->checksum;
529}
530
4c4427a7 531void ffio_init_checksum(AVIOContext *s,
7f37f568
DB
532 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
533 unsigned long checksum)
534{
1dd1c1c8
LB
535 s->update_checksum = update_checksum;
536 if (s->update_checksum) {
537 s->checksum = checksum;
538 s->checksum_ptr = s->buf_ptr;
465e1dad 539 }
ee9f36a8
MN
540}
541
abba3dd1 542/* XXX: put an inline version */
b7effd4e 543int avio_r8(AVIOContext *s)
de6d9b64 544{
3aa13da9 545 if (s->buf_ptr >= s->buf_end)
de6d9b64 546 fill_buffer(s);
853a0fcc
RD
547 if (s->buf_ptr < s->buf_end)
548 return *s->buf_ptr++;
549 return 0;
de6d9b64
FB
550}
551
b7effd4e 552int avio_read(AVIOContext *s, unsigned char *buf, int size)
de6d9b64
FB
553{
554 int len, size1;
555
556 size1 = size;
557 while (size > 0) {
558 len = s->buf_end - s->buf_ptr;
559 if (len > size)
560 len = size;
a0b7e289 561 if (len == 0 || s->write_flag) {
3e2b6358 562 if(size > s->buffer_size && !s->update_checksum){
602dd8f6 563 if(s->read_packet)
c66f53cf 564 len = s->read_packet(s->opaque, buf, size);
3e2b6358
MN
565 if (len <= 0) {
566 /* do not modify buffer if EOF reached so that a seek back can
567 be done without rereading data */
568 s->eof_reached = 1;
569 if(len<0)
570 s->error= len;
571 break;
572 } else {
573 s->pos += len;
574 size -= len;
575 buf += len;
576 s->buf_ptr = s->buffer;
577 s->buf_end = s->buffer/* + len*/;
578 }
1dd1c1c8 579 } else {
3e2b6358
MN
580 fill_buffer(s);
581 len = s->buf_end - s->buf_ptr;
582 if (len == 0)
583 break;
584 }
de6d9b64
FB
585 } else {
586 memcpy(buf, s->buf_ptr, len);
587 buf += len;
588 s->buf_ptr += len;
589 size -= len;
590 }
591 }
7f7686df 592 if (size1 == size) {
3e68b3ba 593 if (s->error) return s->error;
66e5b1df 594 if (s->eof_reached) return AVERROR_EOF;
7f7686df 595 }
de6d9b64
FB
596 return size1 - size;
597}
598
bff0349d
VG
599int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
600{
601 int ret = avio_read(s, buf, size);
602 if (ret != size)
603 return AVERROR_INVALIDDATA;
604 return ret;
605}
606
5afe1d27 607int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
daf1e0d3
BA
608{
609 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
610 *data = s->buf_ptr;
611 s->buf_ptr += size;
612 return size;
613 } else {
614 *data = buf;
615 return avio_read(s, buf, size);
616 }
617}
618
b3db9cee 619int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
e15dec10
LS
620{
621 int len;
115329f1 622
1dd1c1c8 623 if (size < 0)
0ecca7a4 624 return -1;
e15dec10 625
a0b7e289
MS
626 if (s->read_packet && s->write_flag) {
627 len = s->read_packet(s->opaque, buf, size);
628 if (len > 0)
629 s->pos += len;
630 return len;
631 }
632
e15dec10
LS
633 len = s->buf_end - s->buf_ptr;
634 if (len == 0) {
53c25ee0
MS
635 /* Reset the buf_end pointer to the start of the buffer, to make sure
636 * the fill_buffer call tries to read as much data as fits into the
637 * full buffer, instead of just what space is left after buf_end.
638 * This avoids returning partial packets at the end of the buffer,
639 * for packet based inputs.
640 */
641 s->buf_end = s->buf_ptr = s->buffer;
e15dec10
LS
642 fill_buffer(s);
643 len = s->buf_end - s->buf_ptr;
644 }
645 if (len > size)
646 len = size;
647 memcpy(buf, s->buf_ptr, len);
648 s->buf_ptr += len;
7f7686df 649 if (!len) {
3e68b3ba 650 if (s->error) return s->error;
66e5b1df 651 if (s->eof_reached) return AVERROR_EOF;
7f7686df 652 }
e15dec10
LS
653 return len;
654}
655
b7effd4e 656unsigned int avio_rl16(AVIOContext *s)
de6d9b64
FB
657{
658 unsigned int val;
b7effd4e
AK
659 val = avio_r8(s);
660 val |= avio_r8(s) << 8;
de6d9b64
FB
661 return val;
662}
663
b7effd4e 664unsigned int avio_rl24(AVIOContext *s)
ea395e8c
AJ
665{
666 unsigned int val;
b7effd4e
AK
667 val = avio_rl16(s);
668 val |= avio_r8(s) << 16;
ea395e8c
AJ
669 return val;
670}
671
b7effd4e 672unsigned int avio_rl32(AVIOContext *s)
de6d9b64
FB
673{
674 unsigned int val;
b7effd4e
AK
675 val = avio_rl16(s);
676 val |= avio_rl16(s) << 16;
de6d9b64
FB
677 return val;
678}
679
b7effd4e 680uint64_t avio_rl64(AVIOContext *s)
de6d9b64 681{
0c1a9eda 682 uint64_t val;
b7effd4e
AK
683 val = (uint64_t)avio_rl32(s);
684 val |= (uint64_t)avio_rl32(s) << 32;
de6d9b64
FB
685 return val;
686}
687
b7effd4e 688unsigned int avio_rb16(AVIOContext *s)
de6d9b64
FB
689{
690 unsigned int val;
b7effd4e
AK
691 val = avio_r8(s) << 8;
692 val |= avio_r8(s);
de6d9b64
FB
693 return val;
694}
695
b7effd4e 696unsigned int avio_rb24(AVIOContext *s)
de6d9b64
FB
697{
698 unsigned int val;
b7effd4e
AK
699 val = avio_rb16(s) << 8;
700 val |= avio_r8(s);
de6d9b64
FB
701 return val;
702}
b7effd4e 703unsigned int avio_rb32(AVIOContext *s)
a254c574
MN
704{
705 unsigned int val;
b7effd4e
AK
706 val = avio_rb16(s) << 16;
707 val |= avio_rb16(s);
a254c574
MN
708 return val;
709}
de6d9b64 710
ae628ec1 711int ff_get_line(AVIOContext *s, char *buf, int maxlen)
7c892951
AJ
712{
713 int i = 0;
714 char c;
715
716 do {
b7effd4e 717 c = avio_r8(s);
a43416a5 718 if (c && i < maxlen-1)
7c892951
AJ
719 buf[i++] = c;
720 } while (c != '\n' && c);
721
722 buf[i] = 0;
a43416a5 723 return i;
7c892951
AJ
724}
725
41d8555f
RD
726int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
727{
728 int i;
729
ab294069
AC
730 if (buflen <= 0)
731 return AVERROR(EINVAL);
41d8555f
RD
732 // reserve 1 byte for terminating 0
733 buflen = FFMIN(buflen - 1, maxlen);
734 for (i = 0; i < buflen; i++)
735 if (!(buf[i] = avio_r8(s)))
736 return i + 1;
ab294069 737 buf[i] = 0;
41d8555f
RD
738 for (; i < maxlen; i++)
739 if (!avio_r8(s))
740 return i + 1;
741 return maxlen;
742}
743
93b78d12 744#define GET_STR16(type, read) \
ae628ec1 745 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
93b78d12
AK
746{\
747 char* q = buf;\
748 int ret = 0;\
ab294069
AC
749 if (buflen <= 0) \
750 return AVERROR(EINVAL); \
93b78d12
AK
751 while (ret + 1 < maxlen) {\
752 uint8_t tmp;\
753 uint32_t ch;\
754 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
755 if (!ch)\
756 break;\
757 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
758 }\
759 *q = 0;\
760 return ret;\
761}\
762
b7effd4e
AK
763GET_STR16(le, avio_rl16)
764GET_STR16(be, avio_rb16)
93b78d12
AK
765
766#undef GET_STR16
767
b7effd4e 768uint64_t avio_rb64(AVIOContext *s)
de6d9b64 769{
0c1a9eda 770 uint64_t val;
b7effd4e
AK
771 val = (uint64_t)avio_rb32(s) << 32;
772 val |= (uint64_t)avio_rb32(s);
de6d9b64
FB
773 return val;
774}
775
4839c192 776uint64_t ffio_read_varlen(AVIOContext *bc){
897d3eef
KS
777 uint64_t val = 0;
778 int tmp;
779
780 do{
b7effd4e 781 tmp = avio_r8(bc);
897d3eef
KS
782 val= (val<<7) + (tmp&127);
783 }while(tmp&128);
784 return val;
785}
786
cae448cf
AK
787static int io_read_packet(void *opaque, uint8_t *buf, int buf_size)
788{
789 AVIOInternal *internal = opaque;
790 return ffurl_read(internal->h, buf, buf_size);
791}
792
793static int io_write_packet(void *opaque, uint8_t *buf, int buf_size)
794{
795 AVIOInternal *internal = opaque;
796 return ffurl_write(internal->h, buf, buf_size);
797}
798
799static int64_t io_seek(void *opaque, int64_t offset, int whence)
800{
801 AVIOInternal *internal = opaque;
802 return ffurl_seek(internal->h, offset, whence);
803}
804
805static int io_read_pause(void *opaque, int pause)
806{
807 AVIOInternal *internal = opaque;
808 if (!internal->h->prot->url_read_pause)
809 return AVERROR(ENOSYS);
810 return internal->h->prot->url_read_pause(internal->h, pause);
811}
812
813static int64_t io_read_seek(void *opaque, int stream_index, int64_t timestamp, int flags)
814{
815 AVIOInternal *internal = opaque;
816 if (!internal->h->prot->url_read_seek)
817 return AVERROR(ENOSYS);
818 return internal->h->prot->url_read_seek(internal->h, stream_index, timestamp, flags);
819}
820
724f6a0f 821int ffio_fdopen(AVIOContext **s, URLContext *h)
de6d9b64 822{
cae448cf
AK
823 AVIOInternal *internal = NULL;
824 uint8_t *buffer = NULL;
abba3dd1 825 int buffer_size, max_packet_size;
de6d9b64 826
5958df34 827 max_packet_size = h->max_packet_size;
abba3dd1
FB
828 if (max_packet_size) {
829 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
830 } else {
831 buffer_size = IO_BUFFER_SIZE;
832 }
1ea4f593 833 buffer = av_malloc(buffer_size);
de6d9b64 834 if (!buffer)
8fa36ae0 835 return AVERROR(ENOMEM);
de6d9b64 836
cae448cf
AK
837 internal = av_mallocz(sizeof(*internal));
838 if (!internal)
839 goto fail;
840
ec4c4839 841 internal->class = &io_priv_class;
cae448cf
AK
842 internal->h = h;
843
ec4c4839
AK
844 av_opt_set_defaults(internal);
845
cae448cf
AK
846 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE,
847 internal, io_read_packet, io_write_packet, io_seek);
848 if (!*s)
849 goto fail;
850
8978feda 851 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
899681cd 852 (*s)->max_packet_size = max_packet_size;
e7e4810a 853 if(h->prot) {
cae448cf
AK
854 (*s)->read_pause = io_read_pause;
855 (*s)->read_seek = io_read_seek;
8ea35af7
AK
856
857 if (h->prot->url_read_seek)
858 (*s)->seekable |= AVIO_SEEKABLE_TIME;
e7e4810a 859 }
c86d8aed 860 (*s)->av_class = &ff_avio_class;
de6d9b64 861 return 0;
cae448cf 862fail:
ec4c4839
AK
863 if (internal)
864 av_opt_free(internal);
cae448cf
AK
865 av_freep(&internal);
866 av_freep(&buffer);
867 return AVERROR(ENOMEM);
de6d9b64
FB
868}
869
59f65d95 870int ffio_set_buf_size(AVIOContext *s, int buf_size)
de6d9b64 871{
0c1a9eda 872 uint8_t *buffer;
1ea4f593 873 buffer = av_malloc(buf_size);
de6d9b64 874 if (!buffer)
8fa36ae0 875 return AVERROR(ENOMEM);
de6d9b64 876
1ea4f593 877 av_free(s->buffer);
de6d9b64
FB
878 s->buffer = buffer;
879 s->buffer_size = buf_size;
880 s->buf_ptr = buffer;
59d96941 881 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
770d9daf
BA
882 return 0;
883}
884
ae628ec1 885static int url_resetbuf(AVIOContext *s, int flags)
770d9daf 886{
59d96941 887 assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
770d9daf 888
59d96941 889 if (flags & AVIO_FLAG_WRITE) {
770d9daf
BA
890 s->buf_end = s->buffer + s->buffer_size;
891 s->write_flag = 1;
892 } else {
893 s->buf_end = s->buffer;
894 s->write_flag = 0;
895 }
de6d9b64
FB
896 return 0;
897}
898
f1ef2cd9 899int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
01d91b9b
TH
900{
901 int64_t buffer_start;
902 int buffer_size;
496c645d 903 int overlap, new_size, alloc_size;
01d91b9b
TH
904
905 if (s->write_flag)
906 return AVERROR(EINVAL);
907
908 buffer_size = s->buf_end - s->buffer;
909
910 /* the buffers must touch or overlap */
911 if ((buffer_start = s->pos - buffer_size) > buf_size)
912 return AVERROR(EINVAL);
913
914 overlap = buf_size - buffer_start;
915 new_size = buf_size + buffer_size - overlap;
916
496c645d
MS
917 alloc_size = FFMAX(s->buffer_size, new_size);
918 if (alloc_size > buf_size)
919 if (!(buf = av_realloc(buf, alloc_size)))
01d91b9b
TH
920 return AVERROR(ENOMEM);
921
496c645d 922 if (new_size > buf_size) {
01d91b9b
TH
923 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
924 buf_size = new_size;
925 }
926
927 av_free(s->buffer);
928 s->buf_ptr = s->buffer = buf;
496c645d
MS
929 s->buffer_size = alloc_size;
930 s->pos = buf_size;
01d91b9b
TH
931 s->buf_end = s->buf_ptr + buf_size;
932 s->eof_reached = 0;
933 s->must_flush = 0;
934
935 return 0;
936}
937
22a3212e 938int avio_open(AVIOContext **s, const char *filename, int flags)
de6d9b64 939{
1dee0aca
MS
940 return avio_open2(s, filename, flags, NULL, NULL);
941}
942
943int avio_open2(AVIOContext **s, const char *filename, int flags,
944 const AVIOInterruptCB *int_cb, AVDictionary **options)
945{
8c0ceafb
AK
946 AVIOInternal *internal;
947 const URLProtocol **protocols;
ec4c4839
AK
948 char *proto_whitelist = NULL, *proto_blacklist = NULL;
949 AVDictionaryEntry *e;
de6d9b64
FB
950 URLContext *h;
951 int err;
952
ec4c4839
AK
953 if (options) {
954 e = av_dict_get(*options, "protocol_whitelist", NULL, 0);
955 if (e)
956 proto_whitelist = e->value;
957 e = av_dict_get(*options, "protocol_blacklist", NULL, 0);
958 if (e)
959 proto_blacklist = e->value;
960 }
961
962 protocols = ffurl_get_protocols(proto_whitelist, proto_blacklist);
8c0ceafb
AK
963 if (!protocols)
964 return AVERROR(ENOMEM);
965
fab8156b 966 err = ffurl_open(&h, filename, flags, int_cb, options, protocols, NULL);
8c0ceafb
AK
967 if (err < 0) {
968 av_freep(&protocols);
de6d9b64 969 return err;
8c0ceafb
AK
970 }
971
724f6a0f 972 err = ffio_fdopen(s, h);
de6d9b64 973 if (err < 0) {
e52a9145 974 ffurl_close(h);
8c0ceafb 975 av_freep(&protocols);
de6d9b64
FB
976 return err;
977 }
8c0ceafb
AK
978
979 internal = (*s)->opaque;
980 internal->protocols = protocols;
981
ec4c4839
AK
982 if (options) {
983 err = av_opt_set_dict(internal, options);
984 if (err < 0) {
985 avio_closep(s);
986 return err;
987 }
988 }
989
de6d9b64
FB
990 return 0;
991}
992
22a3212e 993int avio_close(AVIOContext *s)
de6d9b64 994{
cae448cf 995 AVIOInternal *internal;
e1e146a2
LB
996 URLContext *h;
997
998 if (!s)
999 return 0;
115329f1 1000
5d1203f0 1001 avio_flush(s);
cae448cf
AK
1002 internal = s->opaque;
1003 h = internal->h;
1004
ec4c4839
AK
1005 av_opt_free(internal);
1006
8c0ceafb 1007 av_freep(&internal->protocols);
cae448cf 1008 av_freep(&s->opaque);
30939390 1009 av_freep(&s->buffer);
899681cd 1010 av_free(s);
e52a9145 1011 return ffurl_close(h);
de6d9b64
FB
1012}
1013
b522000e
LB
1014int avio_closep(AVIOContext **s)
1015{
1016 int ret = avio_close(*s);
1017 *s = NULL;
1018 return ret;
1019}
1020
d9d86e00 1021int avio_printf(AVIOContext *s, const char *fmt, ...)
abba3dd1
FB
1022{
1023 va_list ap;
1024 char buf[4096];
1025 int ret;
1026
1027 va_start(ap, fmt);
1028 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
1029 va_end(ap);
77eb5504 1030 avio_write(s, buf, strlen(buf));
abba3dd1
FB
1031 return ret;
1032}
1033
ff1ec0c3 1034int avio_pause(AVIOContext *s, int pause)
e7e4810a
BA
1035{
1036 if (!s->read_pause)
1037 return AVERROR(ENOSYS);
502bdf68 1038 return s->read_pause(s->opaque, pause);
e7e4810a
BA
1039}
1040
ff1ec0c3 1041int64_t avio_seek_time(AVIOContext *s, int stream_index,
933e90a6 1042 int64_t timestamp, int flags)
e7e4810a 1043{
bc5c918e 1044 int64_t ret;
e7e4810a
BA
1045 if (!s->read_seek)
1046 return AVERROR(ENOSYS);
cae448cf 1047 ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1dd1c1c8 1048 if (ret >= 0) {
c007c687 1049 int64_t pos;
e7e4810a 1050 s->buf_ptr = s->buf_end; // Flush buffer
cae448cf 1051 pos = s->seek(s->opaque, 0, SEEK_CUR);
c007c687
HC
1052 if (pos >= 0)
1053 s->pos = pos;
1054 else if (pos != AVERROR(ENOSYS))
1055 ret = pos;
e7e4810a
BA
1056 }
1057 return ret;
1058}
1059
abba3dd1
FB
1060/* output in a dynamic buffer */
1061
1062typedef struct DynBuffer {
1063 int pos, size, allocated_size;
0c1a9eda 1064 uint8_t *buffer;
abba3dd1 1065 int io_buffer_size;
0c1a9eda 1066 uint8_t io_buffer[1];
abba3dd1
FB
1067} DynBuffer;
1068
576ae256 1069static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
abba3dd1
FB
1070{
1071 DynBuffer *d = opaque;
6bc03a69 1072 unsigned new_size, new_allocated_size;
115329f1 1073
abba3dd1
FB
1074 /* reallocate buffer if needed */
1075 new_size = d->pos + buf_size;
1076 new_allocated_size = d->allocated_size;
1dd1c1c8 1077 if (new_size < d->pos || new_size > INT_MAX/2)
568e18b1 1078 return -1;
abba3dd1
FB
1079 while (new_size > new_allocated_size) {
1080 if (!new_allocated_size)
1081 new_allocated_size = new_size;
1082 else
115329f1 1083 new_allocated_size += new_allocated_size / 2 + 1;
abba3dd1 1084 }
115329f1 1085
abba3dd1 1086 if (new_allocated_size > d->allocated_size) {
5626f994 1087 int err;
d872fb0f
MS
1088 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1089 d->allocated_size = 0;
1090 d->size = 0;
5626f994 1091 return err;
d872fb0f 1092 }
abba3dd1
FB
1093 d->allocated_size = new_allocated_size;
1094 }
1095 memcpy(d->buffer + d->pos, buf, buf_size);
1096 d->pos = new_size;
1097 if (d->pos > d->size)
1098 d->size = d->pos;
576ae256 1099 return buf_size;
abba3dd1
FB
1100}
1101
576ae256 1102static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
abba3dd1
FB
1103{
1104 unsigned char buf1[4];
576ae256 1105 int ret;
abba3dd1
FB
1106
1107 /* packetized write: output the header */
aa805f94 1108 AV_WB32(buf1, buf_size);
1dd1c1c8
LB
1109 ret = dyn_buf_write(opaque, buf1, 4);
1110 if (ret < 0)
576ae256 1111 return ret;
abba3dd1
FB
1112
1113 /* then the data */
576ae256 1114 return dyn_buf_write(opaque, buf, buf_size);
abba3dd1
FB
1115}
1116
bc5c918e 1117static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
abba3dd1
FB
1118{
1119 DynBuffer *d = opaque;
1120
1121 if (whence == SEEK_CUR)
1122 offset += d->pos;
1123 else if (whence == SEEK_END)
1124 offset += d->size;
1125 if (offset < 0 || offset > 0x7fffffffLL)
1126 return -1;
1127 d->pos = offset;
1128 return 0;
1129}
1130
ae628ec1 1131static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
abba3dd1
FB
1132{
1133 DynBuffer *d;
0c5a43d6 1134 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
115329f1 1135
1dd1c1c8 1136 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
568e18b1 1137 return -1;
dc2c2eea 1138 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
abba3dd1 1139 if (!d)
0e7ceb24 1140 return AVERROR(ENOMEM);
8bea72f7
AK
1141 d->io_buffer_size = io_buffer_size;
1142 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1143 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1144 max_packet_size ? NULL : dyn_buf_seek);
899681cd
BA
1145 if(!*s) {
1146 av_free(d);
1147 return AVERROR(ENOMEM);
1148 }
8bea72f7
AK
1149 (*s)->max_packet_size = max_packet_size;
1150 return 0;
abba3dd1
FB
1151}
1152
b92c5452 1153int avio_open_dyn_buf(AVIOContext **s)
abba3dd1
FB
1154{
1155 return url_open_dyn_buf_internal(s, 0);
1156}
1157
403ee835 1158int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
abba3dd1
FB
1159{
1160 if (max_packet_size <= 0)
1161 return -1;
1162 return url_open_dyn_buf_internal(s, max_packet_size);
1163}
1164
6dc7d80d 1165int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
abba3dd1 1166{
d7f530b0 1167 DynBuffer *d;
abba3dd1 1168 int size;
059a9348 1169 static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
de1807bb
JA
1170 int padding = 0;
1171
d07b51bf
MS
1172 if (!s) {
1173 *pbuffer = NULL;
1174 return 0;
1175 }
1176
de1807bb
JA
1177 /* don't attempt to pad fixed-size packet buffers */
1178 if (!s->max_packet_size) {
77eb5504 1179 avio_write(s, padbuf, sizeof(padbuf));
059a9348 1180 padding = AV_INPUT_BUFFER_PADDING_SIZE;
de1807bb 1181 }
abba3dd1 1182
b7f2fdde 1183 avio_flush(s);
abba3dd1 1184
d7f530b0 1185 d = s->opaque;
abba3dd1
FB
1186 *pbuffer = d->buffer;
1187 size = d->size;
1188 av_free(d);
899681cd 1189 av_free(s);
de1807bb 1190 return size - padding;
abba3dd1 1191}
3627ce2f 1192
8a273a74
MS
1193void ffio_free_dyn_buf(AVIOContext **s)
1194{
1195 uint8_t *tmp;
1196 if (!*s)
1197 return;
1198 avio_close_dyn_buf(*s, &tmp);
1199 av_free(tmp);
1200 *s = NULL;
1201}
1202
3627ce2f
MS
1203static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1204{
1205 DynBuffer *d = opaque;
1206
1207 d->pos += buf_size;
1208 if (d->pos > d->size)
1209 d->size = d->pos;
1210 return buf_size;
1211}
1212
1213int ffio_open_null_buf(AVIOContext **s)
1214{
1215 int ret = url_open_dyn_buf_internal(s, 0);
1216 if (ret >= 0) {
1217 AVIOContext *pb = *s;
1218 pb->write_packet = null_buf_write;
1219 }
1220 return ret;
1221}
1222
1223int ffio_close_null_buf(AVIOContext *s)
1224{
1225 DynBuffer *d = s->opaque;
1226 int size;
1227
1228 avio_flush(s);
1229
1230 size = d->size;
1231 av_free(d);
1232 av_free(s);
1233 return size;
1234}