Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ e731b8d8

History | View | Annotate | Download (24.5 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Buffered I/O for ffmpeg system
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000,2001 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 245976da Diego Biurrun
22
#include "libavutil/crc.h"
23 aa805f94 Reimar Döffinger
#include "libavutil/intreadwrite.h"
24 de6d9b64 Fabrice Bellard
#include "avformat.h"
25 5c91a675 Zdenek Kabelac
#include "avio.h"
26 e731b8d8 Anton Khirnov
#include "avio_internal.h"
27 603e5c0b Michael Chinen
#include "internal.h"
28 abba3dd1 Fabrice Bellard
#include <stdarg.h>
29 de6d9b64 Fabrice Bellard
30
#define IO_BUFFER_SIZE 32768
31
32 7939d0ed Martin Storsjö
/**
33
 * Do seeks within this distance ahead of the current buffer by skipping
34
 * data instead of calling the protocol seek function, for seekable
35
 * protocols.
36
 */
37
#define SHORT_SEEK_THRESHOLD 4096
38
39 ae628ec1 Anton Khirnov
static void fill_buffer(AVIOContext *s);
40 364cacc7 Aurelien Jacobs
#if !FF_API_URL_RESETBUF
41 ae628ec1 Anton Khirnov
static int url_resetbuf(AVIOContext *s, int flags);
42 08580cb0 Benoit Fouet
#endif
43 398f5d3f Michael Niedermayer
44 e731b8d8 Anton Khirnov
int ffio_init_context(AVIOContext *s,
45 de6d9b64 Fabrice Bellard
                  unsigned char *buffer,
46
                  int buffer_size,
47
                  int write_flag,
48
                  void *opaque,
49 0c1a9eda Zdenek Kabelac
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
50 576ae256 Michael Niedermayer
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
51 bc5c918e Diego Biurrun
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
52 de6d9b64 Fabrice Bellard
{
53
    s->buffer = buffer;
54
    s->buffer_size = buffer_size;
55
    s->buf_ptr = buffer;
56
    s->opaque = opaque;
57 9daa4cea Aurelien Jacobs
    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
58 de6d9b64 Fabrice Bellard
    s->write_packet = write_packet;
59
    s->read_packet = read_packet;
60
    s->seek = seek;
61
    s->pos = 0;
62
    s->must_flush = 0;
63
    s->eof_reached = 0;
64 576ae256 Michael Niedermayer
    s->error = 0;
65 de6d9b64 Fabrice Bellard
    s->is_streamed = 0;
66 abba3dd1 Fabrice Bellard
    s->max_packet_size = 0;
67 ee9f36a8 Michael Niedermayer
    s->update_checksum= NULL;
68 942f3bb5 Michael Niedermayer
    if(!read_packet && !write_flag){
69
        s->pos = buffer_size;
70
        s->buf_end = s->buffer + buffer_size;
71
    }
72 e7e4810a Björn Axelsson
    s->read_pause = NULL;
73
    s->read_seek  = NULL;
74 de6d9b64 Fabrice Bellard
    return 0;
75
}
76 115329f1 Diego Biurrun
77 e731b8d8 Anton Khirnov
#if FF_API_OLD_AVIO
78
int init_put_byte(AVIOContext *s,
79
                  unsigned char *buffer,
80
                  int buffer_size,
81
                  int write_flag,
82
                  void *opaque,
83
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
84
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
85
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
86
{
87
    return ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
88
                                read_packet, write_packet, seek);
89
}
90
#endif
91
92 ae628ec1 Anton Khirnov
AVIOContext *av_alloc_put_byte(
93 1e0f3468 Reimar Döffinger
                  unsigned char *buffer,
94
                  int buffer_size,
95
                  int write_flag,
96
                  void *opaque,
97
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
98
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
99 7f37f568 Diego Biurrun
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
100
{
101 ae628ec1 Anton Khirnov
    AVIOContext *s = av_mallocz(sizeof(AVIOContext));
102 e731b8d8 Anton Khirnov
    ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
103 1e0f3468 Reimar Döffinger
                  read_packet, write_packet, seek);
104
    return s;
105
}
106
107 ae628ec1 Anton Khirnov
static void flush_buffer(AVIOContext *s)
108 de6d9b64 Fabrice Bellard
{
109
    if (s->buf_ptr > s->buffer) {
110 576ae256 Michael Niedermayer
        if (s->write_packet && !s->error){
111 c66f53cf Martin Storsjö
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
112 576ae256 Michael Niedermayer
            if(ret < 0){
113
                s->error = ret;
114
            }
115
        }
116 465e1dad Michael Niedermayer
        if(s->update_checksum){
117 ee9f36a8 Michael Niedermayer
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
118
            s->checksum_ptr= s->buffer;
119
        }
120 de6d9b64 Fabrice Bellard
        s->pos += s->buf_ptr - s->buffer;
121
    }
122
    s->buf_ptr = s->buffer;
123
}
124
125 ae628ec1 Anton Khirnov
void put_byte(AVIOContext *s, int b)
126 de6d9b64 Fabrice Bellard
{
127
    *(s->buf_ptr)++ = b;
128 115329f1 Diego Biurrun
    if (s->buf_ptr >= s->buf_end)
129 de6d9b64 Fabrice Bellard
        flush_buffer(s);
130
}
131
132 ae628ec1 Anton Khirnov
void put_nbyte(AVIOContext *s, int b, int count)
133 17ee8f66 Anssi Hannula
{
134
    while (count > 0) {
135
        int len = FFMIN(s->buf_end - s->buf_ptr, count);
136
        memset(s->buf_ptr, b, len);
137
        s->buf_ptr += len;
138
139
        if (s->buf_ptr >= s->buf_end)
140
            flush_buffer(s);
141
142
        count -= len;
143
    }
144
}
145
146 ae628ec1 Anton Khirnov
void put_buffer(AVIOContext *s, const unsigned char *buf, int size)
147 de6d9b64 Fabrice Bellard
{
148
    while (size > 0) {
149 90aa6ace Reimar Döffinger
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
150 de6d9b64 Fabrice Bellard
        memcpy(s->buf_ptr, buf, len);
151
        s->buf_ptr += len;
152
153 115329f1 Diego Biurrun
        if (s->buf_ptr >= s->buf_end)
154 de6d9b64 Fabrice Bellard
            flush_buffer(s);
155
156
        buf += len;
157
        size -= len;
158
    }
159
}
160
161 ae628ec1 Anton Khirnov
void put_flush_packet(AVIOContext *s)
162 de6d9b64 Fabrice Bellard
{
163
    flush_buffer(s);
164
    s->must_flush = 0;
165
}
166
167 ae628ec1 Anton Khirnov
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
168 de6d9b64 Fabrice Bellard
{
169 bc5c918e Diego Biurrun
    int64_t offset1;
170
    int64_t pos;
171 c6a5e087 Tomas Härdin
    int force = whence & AVSEEK_FORCE;
172
    whence &= ~AVSEEK_FORCE;
173 899681cd Björn Axelsson
174
    if(!s)
175
        return AVERROR(EINVAL);
176
177
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
178 de6d9b64 Fabrice Bellard
179
    if (whence != SEEK_CUR && whence != SEEK_SET)
180 8fa36ae0 François Revol
        return AVERROR(EINVAL);
181 115329f1 Diego Biurrun
182 f40d1739 Michael Niedermayer
    if (whence == SEEK_CUR) {
183
        offset1 = pos + (s->buf_ptr - s->buffer);
184
        if (offset == 0)
185
            return offset1;
186
        offset += offset1;
187
    }
188
    offset1 = offset - pos;
189
    if (!s->must_flush &&
190 b3eab9fb Baptiste Coudurier
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
191 f40d1739 Michael Niedermayer
        /* can do the seek inside the buffer */
192
        s->buf_ptr = s->buffer + offset1;
193 7939d0ed Martin Storsjö
    } else if ((s->is_streamed ||
194
               offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
195
               !s->write_flag && offset1 >= 0 &&
196 7a6fe01f Tomas Härdin
              (whence != SEEK_END || force)) {
197 398f5d3f Michael Niedermayer
        while(s->pos < offset && !s->eof_reached)
198
            fill_buffer(s);
199 5cb4b82a Baptiste Coudurier
        if (s->eof_reached)
200 8d5e638f Stefano Sabatini
            return AVERROR_EOF;
201 398f5d3f Michael Niedermayer
        s->buf_ptr = s->buf_end + offset - s->pos;
202 f40d1739 Michael Niedermayer
    } else {
203 9d30e068 Stefano Sabatini
        int64_t res;
204 68fcdbf1 Ronald S. Bultje
205 b250f9c6 Aurelien Jacobs
#if CONFIG_MUXERS || CONFIG_NETWORK
206 f40d1739 Michael Niedermayer
        if (s->write_flag) {
207 de6d9b64 Fabrice Bellard
            flush_buffer(s);
208
            s->must_flush = 1;
209
        }
210 b250f9c6 Aurelien Jacobs
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
211 9d30e068 Stefano Sabatini
        if (!s->seek)
212
            return AVERROR(EPIPE);
213
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
214 68fcdbf1 Ronald S. Bultje
            return res;
215 39a127bf Baptiste Coudurier
        if (!s->write_flag)
216
            s->buf_end = s->buffer;
217
        s->buf_ptr = s->buffer;
218 f40d1739 Michael Niedermayer
        s->pos = offset;
219 de6d9b64 Fabrice Bellard
    }
220 f40d1739 Michael Niedermayer
    s->eof_reached = 0;
221 de6d9b64 Fabrice Bellard
    return offset;
222
}
223
224 ae628ec1 Anton Khirnov
int url_fskip(AVIOContext *s, int64_t offset)
225 de6d9b64 Fabrice Bellard
{
226 d5e9ddeb Martin Storsjö
    int64_t ret = url_fseek(s, offset, SEEK_CUR);
227
    return ret < 0 ? ret : 0;
228 de6d9b64 Fabrice Bellard
}
229
230 ae628ec1 Anton Khirnov
int64_t url_ftell(AVIOContext *s)
231 de6d9b64 Fabrice Bellard
{
232
    return url_fseek(s, 0, SEEK_CUR);
233
}
234
235 ae628ec1 Anton Khirnov
int64_t url_fsize(AVIOContext *s)
236 a965c478 Aurelien Jacobs
{
237 bc5c918e Diego Biurrun
    int64_t size;
238 115329f1 Diego Biurrun
239 899681cd Björn Axelsson
    if(!s)
240
        return AVERROR(EINVAL);
241
242 a965c478 Aurelien Jacobs
    if (!s->seek)
243 4c4ef3db Stefano Sabatini
        return AVERROR(ENOSYS);
244 8e287af0 Michael Niedermayer
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
245
    if(size<0){
246 8f42f523 Ronald S. Bultje
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
247
            return size;
248
        size++;
249 1ae2c5f2 Michael Niedermayer
        s->seek(s->opaque, s->pos, SEEK_SET);
250 8e287af0 Michael Niedermayer
    }
251 a965c478 Aurelien Jacobs
    return size;
252
}
253
254 ae628ec1 Anton Khirnov
int url_feof(AVIOContext *s)
255 de6d9b64 Fabrice Bellard
{
256 899681cd Björn Axelsson
    if(!s)
257
        return 0;
258 de6d9b64 Fabrice Bellard
    return s->eof_reached;
259
}
260
261 ae628ec1 Anton Khirnov
int url_ferror(AVIOContext *s)
262 576ae256 Michael Niedermayer
{
263 899681cd Björn Axelsson
    if(!s)
264
        return 0;
265 576ae256 Michael Niedermayer
    return s->error;
266
}
267
268 ae628ec1 Anton Khirnov
void put_le32(AVIOContext *s, unsigned int val)
269 de6d9b64 Fabrice Bellard
{
270
    put_byte(s, val);
271
    put_byte(s, val >> 8);
272
    put_byte(s, val >> 16);
273
    put_byte(s, val >> 24);
274
}
275
276 ae628ec1 Anton Khirnov
void put_be32(AVIOContext *s, unsigned int val)
277 de6d9b64 Fabrice Bellard
{
278
    put_byte(s, val >> 24);
279
    put_byte(s, val >> 16);
280
    put_byte(s, val >> 8);
281
    put_byte(s, val);
282
}
283
284 4efd5cf3 Anton Khirnov
#if FF_API_OLD_AVIO
285 ae628ec1 Anton Khirnov
void put_strz(AVIOContext *s, const char *str)
286 75bdb984 Philip Gladstone
{
287 4efd5cf3 Anton Khirnov
    avio_put_str(s, str);
288
}
289
#endif
290
291 ae628ec1 Anton Khirnov
int avio_put_str(AVIOContext *s, const char *str)
292 4efd5cf3 Anton Khirnov
{
293
    int len = 1;
294
    if (str) {
295
        len += strlen(str);
296
        put_buffer(s, (const unsigned char *) str, len);
297
    } else
298 75bdb984 Philip Gladstone
        put_byte(s, 0);
299 4efd5cf3 Anton Khirnov
    return len;
300 75bdb984 Philip Gladstone
}
301
302 ae628ec1 Anton Khirnov
int avio_put_str16le(AVIOContext *s, const char *str)
303 dccbd97d Anton Khirnov
{
304
    const uint8_t *q = str;
305
    int ret = 0;
306
307
    while (*q) {
308
        uint32_t ch;
309
        uint16_t tmp;
310
311
        GET_UTF8(ch, *q++, break;)
312
        PUT_UTF16(ch, tmp, put_le16(s, tmp);ret += 2;)
313
    }
314
    put_le16(s, 0);
315
    ret += 2;
316
    return ret;
317
}
318
319 603e5c0b Michael Chinen
int ff_get_v_length(uint64_t val){
320
    int i=1;
321
322
    while(val>>=7)
323
        i++;
324
325
    return i;
326
}
327
328 ae628ec1 Anton Khirnov
void ff_put_v(AVIOContext *bc, uint64_t val){
329 603e5c0b Michael Chinen
    int i= ff_get_v_length(val);
330
331
    while(--i>0)
332
        put_byte(bc, 128 | (val>>(7*i)));
333
334
    put_byte(bc, val&127);
335
}
336
337 ae628ec1 Anton Khirnov
void put_le64(AVIOContext *s, uint64_t val)
338 de6d9b64 Fabrice Bellard
{
339 0c1a9eda Zdenek Kabelac
    put_le32(s, (uint32_t)(val & 0xffffffff));
340
    put_le32(s, (uint32_t)(val >> 32));
341 de6d9b64 Fabrice Bellard
}
342
343 ae628ec1 Anton Khirnov
void put_be64(AVIOContext *s, uint64_t val)
344 de6d9b64 Fabrice Bellard
{
345 0c1a9eda Zdenek Kabelac
    put_be32(s, (uint32_t)(val >> 32));
346
    put_be32(s, (uint32_t)(val & 0xffffffff));
347 de6d9b64 Fabrice Bellard
}
348
349 ae628ec1 Anton Khirnov
void put_le16(AVIOContext *s, unsigned int val)
350 de6d9b64 Fabrice Bellard
{
351
    put_byte(s, val);
352
    put_byte(s, val >> 8);
353
}
354
355 ae628ec1 Anton Khirnov
void put_be16(AVIOContext *s, unsigned int val)
356 de6d9b64 Fabrice Bellard
{
357
    put_byte(s, val >> 8);
358
    put_byte(s, val);
359
}
360
361 ae628ec1 Anton Khirnov
void put_le24(AVIOContext *s, unsigned int val)
362 ea395e8c Aurelien Jacobs
{
363
    put_le16(s, val & 0xffff);
364
    put_byte(s, val >> 16);
365
}
366
367 ae628ec1 Anton Khirnov
void put_be24(AVIOContext *s, unsigned int val)
368 a254c574 Michael Niedermayer
{
369
    put_be16(s, val >> 8);
370
    put_byte(s, val);
371
}
372
373 ae628ec1 Anton Khirnov
void put_tag(AVIOContext *s, const char *tag)
374 de6d9b64 Fabrice Bellard
{
375
    while (*tag) {
376
        put_byte(s, *tag++);
377
    }
378
}
379
380
/* Input stream */
381
382 ae628ec1 Anton Khirnov
static void fill_buffer(AVIOContext *s)
383 de6d9b64 Fabrice Bellard
{
384 f5edd874 Jindřich Makovička
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
385 4589dc5c Michael Niedermayer
    int len= s->buffer_size - (dst - s->buffer);
386 01d91b9b Tomas Härdin
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
387 4589dc5c Michael Niedermayer
388 abba3dd1 Fabrice Bellard
    /* no need to do anything if EOF already reached */
389
    if (s->eof_reached)
390
        return;
391 ee9f36a8 Michael Niedermayer
392 4589dc5c Michael Niedermayer
    if(s->update_checksum && dst == s->buffer){
393 0f0680c5 Michael Niedermayer
        if(s->buf_end > s->checksum_ptr)
394
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
395 ee9f36a8 Michael Niedermayer
        s->checksum_ptr= s->buffer;
396
    }
397
398 01d91b9b Tomas Härdin
    /* make buffer smaller in case it ended up large after probing */
399
    if (s->buffer_size > max_buffer_size) {
400
        url_setbufsize(s, max_buffer_size);
401
402
        s->checksum_ptr = dst = s->buffer;
403
        len = s->buffer_size;
404
    }
405
406 602dd8f6 Benoit Fouet
    if(s->read_packet)
407 c66f53cf Martin Storsjö
        len = s->read_packet(s->opaque, dst, len);
408 4589dc5c Michael Niedermayer
    else
409
        len = 0;
410 abba3dd1 Fabrice Bellard
    if (len <= 0) {
411
        /* do not modify buffer if EOF reached so that a seek back can
412
           be done without rereading data */
413 de6d9b64 Fabrice Bellard
        s->eof_reached = 1;
414 5f097703 Alex Beregszaszi
        if(len<0)
415
            s->error= len;
416 abba3dd1 Fabrice Bellard
    } else {
417
        s->pos += len;
418 4589dc5c Michael Niedermayer
        s->buf_ptr = dst;
419
        s->buf_end = dst + len;
420 de6d9b64 Fabrice Bellard
    }
421
}
422
423 7f37f568 Diego Biurrun
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
424
                                    unsigned int len)
425
{
426 3abe5fbd Aurelien Jacobs
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
427 992e78f5 Baptiste Coudurier
}
428
429 ae628ec1 Anton Khirnov
unsigned long get_checksum(AVIOContext *s)
430 7f37f568 Diego Biurrun
{
431 ee9f36a8 Michael Niedermayer
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
432 465e1dad Michael Niedermayer
    s->update_checksum= NULL;
433 ee9f36a8 Michael Niedermayer
    return s->checksum;
434
}
435
436 ae628ec1 Anton Khirnov
void init_checksum(AVIOContext *s,
437 7f37f568 Diego Biurrun
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
438
                   unsigned long checksum)
439
{
440 ee9f36a8 Michael Niedermayer
    s->update_checksum= update_checksum;
441 465e1dad Michael Niedermayer
    if(s->update_checksum){
442 ccb15994 Michael Niedermayer
        s->checksum= checksum;
443 465e1dad Michael Niedermayer
        s->checksum_ptr= s->buf_ptr;
444
    }
445 ee9f36a8 Michael Niedermayer
}
446
447 abba3dd1 Fabrice Bellard
/* XXX: put an inline version */
448 ae628ec1 Anton Khirnov
int get_byte(AVIOContext *s)
449 de6d9b64 Fabrice Bellard
{
450 3aa13da9 Reimar Döffinger
    if (s->buf_ptr >= s->buf_end)
451 de6d9b64 Fabrice Bellard
        fill_buffer(s);
452 853a0fcc Reimar Döffinger
    if (s->buf_ptr < s->buf_end)
453
        return *s->buf_ptr++;
454
    return 0;
455 de6d9b64 Fabrice Bellard
}
456
457 ae628ec1 Anton Khirnov
int url_fgetc(AVIOContext *s)
458 abba3dd1 Fabrice Bellard
{
459 3aa13da9 Reimar Döffinger
    if (s->buf_ptr >= s->buf_end)
460 abba3dd1 Fabrice Bellard
        fill_buffer(s);
461 853a0fcc Reimar Döffinger
    if (s->buf_ptr < s->buf_end)
462
        return *s->buf_ptr++;
463
    return URL_EOF;
464 abba3dd1 Fabrice Bellard
}
465
466 ae628ec1 Anton Khirnov
int get_buffer(AVIOContext *s, unsigned char *buf, int size)
467 de6d9b64 Fabrice Bellard
{
468
    int len, size1;
469
470
    size1 = size;
471
    while (size > 0) {
472
        len = s->buf_end - s->buf_ptr;
473
        if (len > size)
474
            len = size;
475
        if (len == 0) {
476 3e2b6358 Michael Niedermayer
            if(size > s->buffer_size && !s->update_checksum){
477 602dd8f6 Benoit Fouet
                if(s->read_packet)
478 c66f53cf Martin Storsjö
                    len = s->read_packet(s->opaque, buf, size);
479 3e2b6358 Michael Niedermayer
                if (len <= 0) {
480
                    /* do not modify buffer if EOF reached so that a seek back can
481
                    be done without rereading data */
482
                    s->eof_reached = 1;
483
                    if(len<0)
484
                        s->error= len;
485
                    break;
486
                } else {
487
                    s->pos += len;
488
                    size -= len;
489
                    buf += len;
490
                    s->buf_ptr = s->buffer;
491
                    s->buf_end = s->buffer/* + len*/;
492
                }
493
            }else{
494
                fill_buffer(s);
495
                len = s->buf_end - s->buf_ptr;
496
                if (len == 0)
497
                    break;
498
            }
499 de6d9b64 Fabrice Bellard
        } else {
500
            memcpy(buf, s->buf_ptr, len);
501
            buf += len;
502
            s->buf_ptr += len;
503
            size -= len;
504
        }
505
    }
506 7f7686df Reimar Döffinger
    if (size1 == size) {
507
        if (url_ferror(s)) return url_ferror(s);
508
        if (url_feof(s))   return AVERROR_EOF;
509
    }
510 de6d9b64 Fabrice Bellard
    return size1 - size;
511
}
512
513 ae628ec1 Anton Khirnov
int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
514 e15dec10 Leon van Stuivenberg
{
515
    int len;
516 115329f1 Diego Biurrun
517 0ecca7a4 Michael Niedermayer
    if(size<0)
518
        return -1;
519 e15dec10 Leon van Stuivenberg
520
    len = s->buf_end - s->buf_ptr;
521
    if (len == 0) {
522
        fill_buffer(s);
523
        len = s->buf_end - s->buf_ptr;
524
    }
525
    if (len > size)
526
        len = size;
527
    memcpy(buf, s->buf_ptr, len);
528
    s->buf_ptr += len;
529 7f7686df Reimar Döffinger
    if (!len) {
530
        if (url_ferror(s)) return url_ferror(s);
531
        if (url_feof(s))   return AVERROR_EOF;
532
    }
533 e15dec10 Leon van Stuivenberg
    return len;
534
}
535
536 ae628ec1 Anton Khirnov
unsigned int get_le16(AVIOContext *s)
537 de6d9b64 Fabrice Bellard
{
538
    unsigned int val;
539
    val = get_byte(s);
540
    val |= get_byte(s) << 8;
541
    return val;
542
}
543
544 ae628ec1 Anton Khirnov
unsigned int get_le24(AVIOContext *s)
545 ea395e8c Aurelien Jacobs
{
546
    unsigned int val;
547
    val = get_le16(s);
548
    val |= get_byte(s) << 16;
549
    return val;
550
}
551
552 ae628ec1 Anton Khirnov
unsigned int get_le32(AVIOContext *s)
553 de6d9b64 Fabrice Bellard
{
554
    unsigned int val;
555 a254c574 Michael Niedermayer
    val = get_le16(s);
556
    val |= get_le16(s) << 16;
557 de6d9b64 Fabrice Bellard
    return val;
558
}
559
560 ae628ec1 Anton Khirnov
uint64_t get_le64(AVIOContext *s)
561 de6d9b64 Fabrice Bellard
{
562 0c1a9eda Zdenek Kabelac
    uint64_t val;
563
    val = (uint64_t)get_le32(s);
564
    val |= (uint64_t)get_le32(s) << 32;
565 de6d9b64 Fabrice Bellard
    return val;
566
}
567
568 ae628ec1 Anton Khirnov
unsigned int get_be16(AVIOContext *s)
569 de6d9b64 Fabrice Bellard
{
570
    unsigned int val;
571
    val = get_byte(s) << 8;
572
    val |= get_byte(s);
573
    return val;
574
}
575
576 ae628ec1 Anton Khirnov
unsigned int get_be24(AVIOContext *s)
577 de6d9b64 Fabrice Bellard
{
578
    unsigned int val;
579 a254c574 Michael Niedermayer
    val = get_be16(s) << 8;
580 de6d9b64 Fabrice Bellard
    val |= get_byte(s);
581
    return val;
582
}
583 ae628ec1 Anton Khirnov
unsigned int get_be32(AVIOContext *s)
584 a254c574 Michael Niedermayer
{
585
    unsigned int val;
586
    val = get_be16(s) << 16;
587
    val |= get_be16(s);
588
    return val;
589
}
590 de6d9b64 Fabrice Bellard
591 ae628ec1 Anton Khirnov
char *get_strz(AVIOContext *s, char *buf, int maxlen)
592 75bdb984 Philip Gladstone
{
593
    int i = 0;
594
    char c;
595
596
    while ((c = get_byte(s))) {
597
        if (i < maxlen-1)
598
            buf[i++] = c;
599
    }
600 115329f1 Diego Biurrun
601 75bdb984 Philip Gladstone
    buf[i] = 0; /* Ensure null terminated, but may be truncated */
602
603
    return buf;
604
}
605
606 ae628ec1 Anton Khirnov
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
607 7c892951 Aurelien Jacobs
{
608
    int i = 0;
609
    char c;
610
611
    do {
612
        c = get_byte(s);
613 a43416a5 Aurelien Jacobs
        if (c && i < maxlen-1)
614 7c892951 Aurelien Jacobs
            buf[i++] = c;
615
    } while (c != '\n' && c);
616
617
    buf[i] = 0;
618 a43416a5 Aurelien Jacobs
    return i;
619 7c892951 Aurelien Jacobs
}
620
621 93b78d12 Anton Khirnov
#define GET_STR16(type, read) \
622 ae628ec1 Anton Khirnov
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
623 93b78d12 Anton Khirnov
{\
624
    char* q = buf;\
625
    int ret = 0;\
626
    while (ret + 1 < maxlen) {\
627
        uint8_t tmp;\
628
        uint32_t ch;\
629
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
630
        if (!ch)\
631
            break;\
632
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
633
    }\
634
    *q = 0;\
635
    return ret;\
636
}\
637
638
GET_STR16(le, get_le16)
639
GET_STR16(be, get_be16)
640
641
#undef GET_STR16
642
643 ae628ec1 Anton Khirnov
uint64_t get_be64(AVIOContext *s)
644 de6d9b64 Fabrice Bellard
{
645 0c1a9eda Zdenek Kabelac
    uint64_t val;
646
    val = (uint64_t)get_be32(s) << 32;
647
    val |= (uint64_t)get_be32(s);
648 de6d9b64 Fabrice Bellard
    return val;
649
}
650
651 ae628ec1 Anton Khirnov
uint64_t ff_get_v(AVIOContext *bc){
652 897d3eef Kostya Shishkov
    uint64_t val = 0;
653
    int tmp;
654
655
    do{
656
        tmp = get_byte(bc);
657
        val= (val<<7) + (tmp&127);
658
    }while(tmp&128);
659
    return val;
660
}
661
662 ae628ec1 Anton Khirnov
int url_fdopen(AVIOContext **s, URLContext *h)
663 de6d9b64 Fabrice Bellard
{
664 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
665 abba3dd1 Fabrice Bellard
    int buffer_size, max_packet_size;
666 de6d9b64 Fabrice Bellard
667 abba3dd1 Fabrice Bellard
    max_packet_size = url_get_max_packet_size(h);
668
    if (max_packet_size) {
669
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
670
    } else {
671
        buffer_size = IO_BUFFER_SIZE;
672
    }
673 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buffer_size);
674 de6d9b64 Fabrice Bellard
    if (!buffer)
675 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
676 de6d9b64 Fabrice Bellard
677 ae628ec1 Anton Khirnov
    *s = av_mallocz(sizeof(AVIOContext));
678 899681cd Björn Axelsson
    if(!*s) {
679
        av_free(buffer);
680
        return AVERROR(ENOMEM);
681
    }
682
683 e731b8d8 Anton Khirnov
    if (ffio_init_context(*s, buffer, buffer_size,
684 ac9fe33d Gildas Bazin
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
685 004050ee Michael Niedermayer
                      url_read, url_write, url_seek) < 0) {
686 1ea4f593 Fabrice Bellard
        av_free(buffer);
687 899681cd Björn Axelsson
        av_freep(s);
688 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
689 de6d9b64 Fabrice Bellard
    }
690 899681cd Björn Axelsson
    (*s)->is_streamed = h->is_streamed;
691
    (*s)->max_packet_size = max_packet_size;
692 e7e4810a Björn Axelsson
    if(h->prot) {
693 502bdf68 Michael Niedermayer
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
694 bc5c918e Diego Biurrun
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
695 e7e4810a Björn Axelsson
    }
696 de6d9b64 Fabrice Bellard
    return 0;
697
}
698
699 ae628ec1 Anton Khirnov
int url_setbufsize(AVIOContext *s, int buf_size)
700 de6d9b64 Fabrice Bellard
{
701 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
702 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buf_size);
703 de6d9b64 Fabrice Bellard
    if (!buffer)
704 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
705 de6d9b64 Fabrice Bellard
706 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
707 de6d9b64 Fabrice Bellard
    s->buffer = buffer;
708
    s->buffer_size = buf_size;
709
    s->buf_ptr = buffer;
710 770d9daf Björn Axelsson
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
711
    return 0;
712
}
713
714 364cacc7 Aurelien Jacobs
#if FF_API_URL_RESETBUF
715 ae628ec1 Anton Khirnov
int url_resetbuf(AVIOContext *s, int flags)
716 08580cb0 Benoit Fouet
#else
717 ae628ec1 Anton Khirnov
static int url_resetbuf(AVIOContext *s, int flags)
718 08580cb0 Benoit Fouet
#endif
719 770d9daf Björn Axelsson
{
720 364cacc7 Aurelien Jacobs
#if FF_API_URL_RESETBUF
721 6fa197e2 Eli Friedman
    if (flags & URL_RDWR)
722 770d9daf Björn Axelsson
        return AVERROR(EINVAL);
723 08580cb0 Benoit Fouet
#else
724
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
725
#endif
726 770d9daf Björn Axelsson
727
    if (flags & URL_WRONLY) {
728
        s->buf_end = s->buffer + s->buffer_size;
729
        s->write_flag = 1;
730
    } else {
731
        s->buf_end = s->buffer;
732
        s->write_flag = 0;
733
    }
734 de6d9b64 Fabrice Bellard
    return 0;
735
}
736
737 ae628ec1 Anton Khirnov
int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
738 01d91b9b Tomas Härdin
{
739
    int64_t buffer_start;
740
    int buffer_size;
741 496c645d Martin Storsjö
    int overlap, new_size, alloc_size;
742 01d91b9b Tomas Härdin
743
    if (s->write_flag)
744
        return AVERROR(EINVAL);
745
746
    buffer_size = s->buf_end - s->buffer;
747
748
    /* the buffers must touch or overlap */
749
    if ((buffer_start = s->pos - buffer_size) > buf_size)
750
        return AVERROR(EINVAL);
751
752
    overlap = buf_size - buffer_start;
753
    new_size = buf_size + buffer_size - overlap;
754
755 496c645d Martin Storsjö
    alloc_size = FFMAX(s->buffer_size, new_size);
756
    if (alloc_size > buf_size)
757
        if (!(buf = av_realloc(buf, alloc_size)))
758 01d91b9b Tomas Härdin
            return AVERROR(ENOMEM);
759
760 496c645d Martin Storsjö
    if (new_size > buf_size) {
761 01d91b9b Tomas Härdin
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
762
        buf_size = new_size;
763
    }
764
765
    av_free(s->buffer);
766
    s->buf_ptr = s->buffer = buf;
767 496c645d Martin Storsjö
    s->buffer_size = alloc_size;
768
    s->pos = buf_size;
769 01d91b9b Tomas Härdin
    s->buf_end = s->buf_ptr + buf_size;
770
    s->eof_reached = 0;
771
    s->must_flush = 0;
772
773
    return 0;
774
}
775
776 ae628ec1 Anton Khirnov
int url_fopen(AVIOContext **s, const char *filename, int flags)
777 de6d9b64 Fabrice Bellard
{
778
    URLContext *h;
779
    int err;
780
781
    err = url_open(&h, filename, flags);
782
    if (err < 0)
783
        return err;
784
    err = url_fdopen(s, h);
785
    if (err < 0) {
786
        url_close(h);
787
        return err;
788
    }
789
    return 0;
790
}
791
792 ae628ec1 Anton Khirnov
int url_fclose(AVIOContext *s)
793 de6d9b64 Fabrice Bellard
{
794
    URLContext *h = s->opaque;
795 115329f1 Diego Biurrun
796 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
797 899681cd Björn Axelsson
    av_free(s);
798 de6d9b64 Fabrice Bellard
    return url_close(h);
799
}
800
801 ae628ec1 Anton Khirnov
URLContext *url_fileno(AVIOContext *s)
802 de6d9b64 Fabrice Bellard
{
803
    return s->opaque;
804
}
805
806 b250f9c6 Aurelien Jacobs
#if CONFIG_MUXERS
807 ae628ec1 Anton Khirnov
int url_fprintf(AVIOContext *s, const char *fmt, ...)
808 abba3dd1 Fabrice Bellard
{
809
    va_list ap;
810
    char buf[4096];
811
    int ret;
812
813
    va_start(ap, fmt);
814
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
815
    va_end(ap);
816
    put_buffer(s, buf, strlen(buf));
817
    return ret;
818
}
819 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
820 abba3dd1 Fabrice Bellard
821 ae628ec1 Anton Khirnov
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
822 abba3dd1 Fabrice Bellard
{
823
    int c;
824
    char *q;
825
826
    c = url_fgetc(s);
827
    if (c == EOF)
828
        return NULL;
829
    q = buf;
830
    for(;;) {
831
        if (c == EOF || c == '\n')
832
            break;
833
        if ((q - buf) < buf_size - 1)
834
            *q++ = c;
835
        c = url_fgetc(s);
836
    }
837
    if (buf_size > 0)
838
        *q = '\0';
839
    return buf;
840
}
841
842 ae628ec1 Anton Khirnov
int url_fget_max_packet_size(AVIOContext *s)
843 abba3dd1 Fabrice Bellard
{
844
    return s->max_packet_size;
845
}
846
847 ae628ec1 Anton Khirnov
int av_url_read_fpause(AVIOContext *s, int pause)
848 e7e4810a Björn Axelsson
{
849
    if (!s->read_pause)
850
        return AVERROR(ENOSYS);
851 502bdf68 Michael Niedermayer
    return s->read_pause(s->opaque, pause);
852 e7e4810a Björn Axelsson
}
853
854 ae628ec1 Anton Khirnov
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
855 7f37f568 Diego Biurrun
                          int64_t timestamp, int flags)
856 e7e4810a Björn Axelsson
{
857
    URLContext *h = s->opaque;
858 bc5c918e Diego Biurrun
    int64_t ret;
859 e7e4810a Björn Axelsson
    if (!s->read_seek)
860
        return AVERROR(ENOSYS);
861
    ret = s->read_seek(h, stream_index, timestamp, flags);
862
    if(ret >= 0) {
863 c007c687 Howard Chu
        int64_t pos;
864 e7e4810a Björn Axelsson
        s->buf_ptr = s->buf_end; // Flush buffer
865 c007c687 Howard Chu
        pos = s->seek(h, 0, SEEK_CUR);
866
        if (pos >= 0)
867
            s->pos = pos;
868
        else if (pos != AVERROR(ENOSYS))
869
            ret = pos;
870 e7e4810a Björn Axelsson
    }
871
    return ret;
872
}
873
874 8fa641f8 Loïc Le Loarer
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
875 b250f9c6 Aurelien Jacobs
 * back to the server even if CONFIG_MUXERS is false. */
876
#if CONFIG_MUXERS || CONFIG_NETWORK
877 de6d9b64 Fabrice Bellard
/* buffer handling */
878 ae628ec1 Anton Khirnov
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
879 de6d9b64 Fabrice Bellard
{
880 899681cd Björn Axelsson
    int ret;
881 ae628ec1 Anton Khirnov
    *s = av_mallocz(sizeof(AVIOContext));
882 899681cd Björn Axelsson
    if(!*s)
883
        return AVERROR(ENOMEM);
884 e731b8d8 Anton Khirnov
    ret = ffio_init_context(*s, buf, buf_size,
885 899681cd Björn Axelsson
                        (flags & URL_WRONLY || flags & URL_RDWR),
886
                        NULL, NULL, NULL, NULL);
887
    if(ret != 0)
888
        av_freep(s);
889
    return ret;
890 de6d9b64 Fabrice Bellard
}
891
892 ae628ec1 Anton Khirnov
int url_close_buf(AVIOContext *s)
893 de6d9b64 Fabrice Bellard
{
894 abba3dd1 Fabrice Bellard
    put_flush_packet(s);
895 de6d9b64 Fabrice Bellard
    return s->buf_ptr - s->buffer;
896
}
897 abba3dd1 Fabrice Bellard
898
/* output in a dynamic buffer */
899
900
typedef struct DynBuffer {
901
    int pos, size, allocated_size;
902 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
903 abba3dd1 Fabrice Bellard
    int io_buffer_size;
904 0c1a9eda Zdenek Kabelac
    uint8_t io_buffer[1];
905 abba3dd1 Fabrice Bellard
} DynBuffer;
906
907 576ae256 Michael Niedermayer
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
908 abba3dd1 Fabrice Bellard
{
909
    DynBuffer *d = opaque;
910 6bc03a69 Reimar Döffinger
    unsigned new_size, new_allocated_size;
911 115329f1 Diego Biurrun
912 abba3dd1 Fabrice Bellard
    /* reallocate buffer if needed */
913
    new_size = d->pos + buf_size;
914
    new_allocated_size = d->allocated_size;
915 568e18b1 Michael Niedermayer
    if(new_size < d->pos || new_size > INT_MAX/2)
916
        return -1;
917 abba3dd1 Fabrice Bellard
    while (new_size > new_allocated_size) {
918
        if (!new_allocated_size)
919
            new_allocated_size = new_size;
920
        else
921 115329f1 Diego Biurrun
            new_allocated_size += new_allocated_size / 2 + 1;
922 abba3dd1 Fabrice Bellard
    }
923 115329f1 Diego Biurrun
924 abba3dd1 Fabrice Bellard
    if (new_allocated_size > d->allocated_size) {
925 b88ba823 Mark Hills
        d->buffer = av_realloc(d->buffer, new_allocated_size);
926
        if(d->buffer == NULL)
927 7ce820ef Reimar Döffinger
             return AVERROR(ENOMEM);
928 abba3dd1 Fabrice Bellard
        d->allocated_size = new_allocated_size;
929
    }
930
    memcpy(d->buffer + d->pos, buf, buf_size);
931
    d->pos = new_size;
932
    if (d->pos > d->size)
933
        d->size = d->pos;
934 576ae256 Michael Niedermayer
    return buf_size;
935 abba3dd1 Fabrice Bellard
}
936
937 576ae256 Michael Niedermayer
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
938 abba3dd1 Fabrice Bellard
{
939
    unsigned char buf1[4];
940 576ae256 Michael Niedermayer
    int ret;
941 abba3dd1 Fabrice Bellard
942
    /* packetized write: output the header */
943 aa805f94 Reimar Döffinger
    AV_WB32(buf1, buf_size);
944 576ae256 Michael Niedermayer
    ret= dyn_buf_write(opaque, buf1, 4);
945
    if(ret < 0)
946
        return ret;
947 abba3dd1 Fabrice Bellard
948
    /* then the data */
949 576ae256 Michael Niedermayer
    return dyn_buf_write(opaque, buf, buf_size);
950 abba3dd1 Fabrice Bellard
}
951
952 bc5c918e Diego Biurrun
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
953 abba3dd1 Fabrice Bellard
{
954
    DynBuffer *d = opaque;
955
956
    if (whence == SEEK_CUR)
957
        offset += d->pos;
958
    else if (whence == SEEK_END)
959
        offset += d->size;
960
    if (offset < 0 || offset > 0x7fffffffLL)
961
        return -1;
962
    d->pos = offset;
963
    return 0;
964
}
965
966 ae628ec1 Anton Khirnov
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
967 abba3dd1 Fabrice Bellard
{
968
    DynBuffer *d;
969 2bdaf78c Reimar Döffinger
    int ret;
970 0c5a43d6 Reimar Döffinger
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
971 115329f1 Diego Biurrun
972 568e18b1 Michael Niedermayer
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
973
        return -1;
974 dc2c2eea Reimar Döffinger
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
975 abba3dd1 Fabrice Bellard
    if (!d)
976 0e7ceb24 Reimar Döffinger
        return AVERROR(ENOMEM);
977 ae628ec1 Anton Khirnov
    *s = av_mallocz(sizeof(AVIOContext));
978 899681cd Björn Axelsson
    if(!*s) {
979
        av_free(d);
980
        return AVERROR(ENOMEM);
981
    }
982 abba3dd1 Fabrice Bellard
    d->io_buffer_size = io_buffer_size;
983 e731b8d8 Anton Khirnov
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
984 115329f1 Diego Biurrun
                        1, d, NULL,
985
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
986 abba3dd1 Fabrice Bellard
                        max_packet_size ? NULL : dyn_buf_seek);
987
    if (ret == 0) {
988 899681cd Björn Axelsson
        (*s)->max_packet_size = max_packet_size;
989
    } else {
990
        av_free(d);
991
        av_freep(s);
992 abba3dd1 Fabrice Bellard
    }
993
    return ret;
994
}
995
996 ae628ec1 Anton Khirnov
int url_open_dyn_buf(AVIOContext **s)
997 abba3dd1 Fabrice Bellard
{
998
    return url_open_dyn_buf_internal(s, 0);
999
}
1000
1001 ae628ec1 Anton Khirnov
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1002 abba3dd1 Fabrice Bellard
{
1003
    if (max_packet_size <= 0)
1004
        return -1;
1005
    return url_open_dyn_buf_internal(s, max_packet_size);
1006
}
1007
1008 ae628ec1 Anton Khirnov
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1009 abba3dd1 Fabrice Bellard
{
1010
    DynBuffer *d = s->opaque;
1011
    int size;
1012 de1807bb Josh Allmann
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1013
    int padding = 0;
1014
1015
    /* don't attempt to pad fixed-size packet buffers */
1016
    if (!s->max_packet_size) {
1017
        put_buffer(s, padbuf, sizeof(padbuf));
1018
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1019
    }
1020 abba3dd1 Fabrice Bellard
1021
    put_flush_packet(s);
1022
1023
    *pbuffer = d->buffer;
1024
    size = d->size;
1025
    av_free(d);
1026 899681cd Björn Axelsson
    av_free(s);
1027 de1807bb Josh Allmann
    return size - padding;
1028 abba3dd1 Fabrice Bellard
}
1029 8fa641f8 Loïc Le Loarer
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */