Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 546031ee

History | View | Annotate | Download (15.8 KB)

1
/*
2
 * Buffered I/O for ffmpeg system
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
#include "avformat.h"
20
#include "avio.h"
21
#include <stdarg.h>
22

    
23
#define IO_BUFFER_SIZE 32768
24

    
25
int init_put_byte(ByteIOContext *s,
26
                  unsigned char *buffer,
27
                  int buffer_size,
28
                  int write_flag,
29
                  void *opaque,
30
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
31
                  void (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
32
                  int (*seek)(void *opaque, offset_t offset, int whence))
33
{
34
    s->buffer = buffer;
35
    s->buffer_size = buffer_size;
36
    s->buf_ptr = buffer;
37
    s->write_flag = write_flag;
38
    if (!s->write_flag) 
39
        s->buf_end = buffer;
40
    else
41
        s->buf_end = buffer + buffer_size;
42
    s->opaque = opaque;
43
    s->write_packet = write_packet;
44
    s->read_packet = read_packet;
45
    s->seek = seek;
46
    s->pos = 0;
47
    s->must_flush = 0;
48
    s->eof_reached = 0;
49
    s->is_streamed = 0;
50
    s->max_packet_size = 0;
51
    return 0;
52
}
53
                  
54

    
55
static void flush_buffer(ByteIOContext *s)
56
{
57
    if (s->buf_ptr > s->buffer) {
58
        if (s->write_packet)
59
            s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
60
        s->pos += s->buf_ptr - s->buffer;
61
    }
62
    s->buf_ptr = s->buffer;
63
}
64

    
65
void put_byte(ByteIOContext *s, int b)
66
{
67
    *(s->buf_ptr)++ = b;
68
    if (s->buf_ptr >= s->buf_end) 
69
        flush_buffer(s);
70
}
71

    
72
void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
73
{
74
    int len;
75

    
76
    while (size > 0) {
77
        len = (s->buf_end - s->buf_ptr);
78
        if (len > size)
79
            len = size;
80
        memcpy(s->buf_ptr, buf, len);
81
        s->buf_ptr += len;
82

    
83
        if (s->buf_ptr >= s->buf_end) 
84
            flush_buffer(s);
85

    
86
        buf += len;
87
        size -= len;
88
    }
89
}
90

    
91
void put_flush_packet(ByteIOContext *s)
92
{
93
    flush_buffer(s);
94
    s->must_flush = 0;
95
}
96

    
97
offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
98
{
99
    offset_t offset1;
100

    
101
    if (whence != SEEK_CUR && whence != SEEK_SET)
102
        return -EINVAL;
103
    
104
    if (s->write_flag) {
105
        if (whence == SEEK_CUR) {
106
            offset1 = s->pos + (s->buf_ptr - s->buffer);
107
            if (offset == 0)
108
                return offset1;
109
            offset += offset1;
110
        }
111
        offset1 = offset - s->pos;
112
        if (!s->must_flush && 
113
            offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
114
            /* can do the seek inside the buffer */
115
            s->buf_ptr = s->buffer + offset1;
116
        } else {
117
            if (!s->seek)
118
                return -EPIPE;
119
            flush_buffer(s);
120
            s->must_flush = 1;
121
            s->buf_ptr = s->buffer;
122
            s->seek(s->opaque, offset, SEEK_SET);
123
            s->pos = offset;
124
        }
125
    } else {
126
        if (whence == SEEK_CUR) {
127
            offset1 = s->pos - (s->buf_end - s->buffer) + (s->buf_ptr - s->buffer);
128
            if (offset == 0)
129
                return offset1;
130
            offset += offset1;
131
        }
132
        offset1 = offset - (s->pos - (s->buf_end - s->buffer));
133
        if (offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
134
            /* can do the seek inside the buffer */
135
            s->buf_ptr = s->buffer + offset1;
136
        } else {
137
            if (!s->seek)
138
                return -EPIPE;
139
            s->buf_ptr = s->buffer;
140
            s->buf_end = s->buffer;
141
            s->seek(s->opaque, offset, SEEK_SET);
142
            s->pos = offset;
143
        }
144
        s->eof_reached = 0;
145
    }
146
    return offset;
147
}
148

    
149
void url_fskip(ByteIOContext *s, offset_t offset)
150
{
151
    url_fseek(s, offset, SEEK_CUR);
152
}
153

    
154
offset_t url_ftell(ByteIOContext *s)
155
{
156
    return url_fseek(s, 0, SEEK_CUR);
157
}
158

    
159
int url_feof(ByteIOContext *s)
160
{
161
    return s->eof_reached;
162
}
163

    
164
void put_le32(ByteIOContext *s, unsigned int val)
165
{
166
    put_byte(s, val);
167
    put_byte(s, val >> 8);
168
    put_byte(s, val >> 16);
169
    put_byte(s, val >> 24);
170
}
171

    
172
void put_be32(ByteIOContext *s, unsigned int val)
173
{
174
    put_byte(s, val >> 24);
175
    put_byte(s, val >> 16);
176
    put_byte(s, val >> 8);
177
    put_byte(s, val);
178
}
179

    
180
/* IEEE format is assumed */
181
void put_be64_double(ByteIOContext *s, double val)
182
{
183
    union {
184
        double d;
185
        uint64_t ull;
186
    } u;
187
    u.d = val;
188
    put_be64(s, u.ull);
189
}
190

    
191
void put_strz(ByteIOContext *s, const char *str)
192
{
193
    if (str)
194
        put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
195
    else
196
        put_byte(s, 0);
197
}
198

    
199
void put_le64(ByteIOContext *s, uint64_t val)
200
{
201
    put_le32(s, (uint32_t)(val & 0xffffffff));
202
    put_le32(s, (uint32_t)(val >> 32));
203
}
204

    
205
void put_be64(ByteIOContext *s, uint64_t val)
206
{
207
    put_be32(s, (uint32_t)(val >> 32));
208
    put_be32(s, (uint32_t)(val & 0xffffffff));
209
}
210

    
211
void put_le16(ByteIOContext *s, unsigned int val)
212
{
213
    put_byte(s, val);
214
    put_byte(s, val >> 8);
215
}
216

    
217
void put_be16(ByteIOContext *s, unsigned int val)
218
{
219
    put_byte(s, val >> 8);
220
    put_byte(s, val);
221
}
222

    
223
void put_tag(ByteIOContext *s, const char *tag)
224
{
225
    while (*tag) {
226
        put_byte(s, *tag++);
227
    }
228
}
229

    
230
/* Input stream */
231

    
232
static void fill_buffer(ByteIOContext *s)
233
{
234
    int len;
235

    
236
    /* no need to do anything if EOF already reached */
237
    if (s->eof_reached)
238
        return;
239
    len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
240
    if (len <= 0) {
241
        /* do not modify buffer if EOF reached so that a seek back can
242
           be done without rereading data */
243
        s->eof_reached = 1;
244
    } else {
245
        s->pos += len;
246
        s->buf_ptr = s->buffer;
247
        s->buf_end = s->buffer + len;
248
    }
249
}
250

    
251
/* NOTE: return 0 if EOF, so you cannot use it if EOF handling is
252
   necessary */
253
/* XXX: put an inline version */
254
int get_byte(ByteIOContext *s)
255
{
256
    if (s->buf_ptr < s->buf_end) {
257
        return *s->buf_ptr++;
258
    } else {
259
        fill_buffer(s);
260
        if (s->buf_ptr < s->buf_end)
261
            return *s->buf_ptr++;
262
        else
263
            return 0;
264
    }
265
}
266

    
267
/* NOTE: return URL_EOF (-1) if EOF */
268
int url_fgetc(ByteIOContext *s)
269
{
270
    if (s->buf_ptr < s->buf_end) {
271
        return *s->buf_ptr++;
272
    } else {
273
        fill_buffer(s);
274
        if (s->buf_ptr < s->buf_end)
275
            return *s->buf_ptr++;
276
        else
277
            return URL_EOF;
278
    }
279
}
280

    
281
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
282
{
283
    int len, size1;
284

    
285
    size1 = size;
286
    while (size > 0) {
287
        len = s->buf_end - s->buf_ptr;
288
        if (len > size)
289
            len = size;
290
        if (len == 0) {
291
            fill_buffer(s);
292
            len = s->buf_end - s->buf_ptr;
293
            if (len == 0)
294
                break;
295
        } else {
296
            memcpy(buf, s->buf_ptr, len);
297
            buf += len;
298
            s->buf_ptr += len;
299
            size -= len;
300
        }
301
    }
302
    return size1 - size;
303
}
304

    
305
unsigned int get_le16(ByteIOContext *s)
306
{
307
    unsigned int val;
308
    val = get_byte(s);
309
    val |= get_byte(s) << 8;
310
    return val;
311
}
312

    
313
unsigned int get_le32(ByteIOContext *s)
314
{
315
    unsigned int val;
316
    val = get_byte(s);
317
    val |= get_byte(s) << 8;
318
    val |= get_byte(s) << 16;
319
    val |= get_byte(s) << 24;
320
    return val;
321
}
322

    
323
uint64_t get_le64(ByteIOContext *s)
324
{
325
    uint64_t val;
326
    val = (uint64_t)get_le32(s);
327
    val |= (uint64_t)get_le32(s) << 32;
328
    return val;
329
}
330

    
331
unsigned int get_be16(ByteIOContext *s)
332
{
333
    unsigned int val;
334
    val = get_byte(s) << 8;
335
    val |= get_byte(s);
336
    return val;
337
}
338

    
339
unsigned int get_be32(ByteIOContext *s)
340
{
341
    unsigned int val;
342
    val = get_byte(s) << 24;
343
    val |= get_byte(s) << 16;
344
    val |= get_byte(s) << 8;
345
    val |= get_byte(s);
346
    return val;
347
}
348

    
349
double get_be64_double(ByteIOContext *s)
350
{
351
    union {
352
        double d;
353
        uint64_t ull;
354
    } u;
355

    
356
    u.ull = get_be64(s);
357
    return u.d;
358
}
359

    
360
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
361
{
362
    int i = 0;
363
    char c;
364

    
365
    while ((c = get_byte(s))) {
366
        if (i < maxlen-1)
367
            buf[i++] = c;
368
    }
369
    
370
    buf[i] = 0; /* Ensure null terminated, but may be truncated */
371

    
372
    return buf;
373
}
374

    
375
uint64_t get_be64(ByteIOContext *s)
376
{
377
    uint64_t val;
378
    val = (uint64_t)get_be32(s) << 32;
379
    val |= (uint64_t)get_be32(s);
380
    return val;
381
}
382

    
383
/* link with avio functions */
384

    
385
static void url_write_packet(void *opaque, uint8_t *buf, int buf_size)
386
{
387
    URLContext *h = opaque;
388
    url_write(h, buf, buf_size);
389
}
390

    
391
static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
392
{
393
    URLContext *h = opaque;
394
    return url_read(h, buf, buf_size);
395
}
396

    
397
static int url_seek_packet(void *opaque, int64_t offset, int whence)
398
{
399
    URLContext *h = opaque;
400
    url_seek(h, offset, whence);
401
    return 0;
402
}
403

    
404
int url_fdopen(ByteIOContext *s, URLContext *h)
405
{
406
    uint8_t *buffer;
407
    int buffer_size, max_packet_size;
408

    
409
    
410
    max_packet_size = url_get_max_packet_size(h);
411
    if (max_packet_size) {
412
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
413
    } else {
414
        buffer_size = IO_BUFFER_SIZE;
415
    }
416
    buffer = av_malloc(buffer_size);
417
    if (!buffer)
418
        return -ENOMEM;
419

    
420
    if (init_put_byte(s, buffer, buffer_size, 
421
                      (h->flags & URL_WRONLY) != 0, h,
422
                      url_read_packet, url_write_packet, url_seek_packet) < 0) {
423
        av_free(buffer);
424
        return -EIO;
425
    }
426
    s->is_streamed = h->is_streamed;
427
    s->max_packet_size = max_packet_size;
428
    return 0;
429
}
430

    
431
/* XXX: must be called before any I/O */
432
int url_setbufsize(ByteIOContext *s, int buf_size)
433
{
434
    uint8_t *buffer;
435
    buffer = av_malloc(buf_size);
436
    if (!buffer)
437
        return -ENOMEM;
438

    
439
    av_free(s->buffer);
440
    s->buffer = buffer;
441
    s->buffer_size = buf_size;
442
    s->buf_ptr = buffer;
443
    if (!s->write_flag) 
444
        s->buf_end = buffer;
445
    else
446
        s->buf_end = buffer + buf_size;
447
    return 0;
448
}
449

    
450
/* NOTE: when opened as read/write, the buffers are only used for
451
   reading */
452
int url_fopen(ByteIOContext *s, const char *filename, int flags)
453
{
454
    URLContext *h;
455
    int err;
456

    
457
    err = url_open(&h, filename, flags);
458
    if (err < 0)
459
        return err;
460
    err = url_fdopen(s, h);
461
    if (err < 0) {
462
        url_close(h);
463
        return err;
464
    }
465
    return 0;
466
}
467

    
468
int url_fclose(ByteIOContext *s)
469
{
470
    URLContext *h = s->opaque;
471
    
472
    av_free(s->buffer);
473
    memset(s, 0, sizeof(ByteIOContext));
474
    return url_close(h);
475
}
476

    
477
URLContext *url_fileno(ByteIOContext *s)
478
{
479
    return s->opaque;
480
}
481

    
482
/* XXX: currently size is limited */
483
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
484
{
485
    va_list ap;
486
    char buf[4096];
487
    int ret;
488

    
489
    va_start(ap, fmt);
490
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
491
    va_end(ap);
492
    put_buffer(s, buf, strlen(buf));
493
    return ret;
494
}
495

    
496
/* note: unlike fgets, the EOL character is not returned and a whole
497
   line is parsed. return NULL if first char read was EOF */
498
char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
499
{
500
    int c;
501
    char *q;
502

    
503
    c = url_fgetc(s);
504
    if (c == EOF)
505
        return NULL;
506
    q = buf;
507
    for(;;) {
508
        if (c == EOF || c == '\n')
509
            break;
510
        if ((q - buf) < buf_size - 1)
511
            *q++ = c;
512
        c = url_fgetc(s);
513
    }
514
    if (buf_size > 0)
515
        *q = '\0';
516
    return buf;
517
}
518

    
519
/* 
520
 * Return the maximum packet size associated to packetized buffered file
521
 * handle. If the file is not packetized (stream like http or file on
522
 * disk), then 0 is returned.
523
 * 
524
 * @param h buffered file handle
525
 * @return maximum packet size in bytes
526
 */
527
int url_fget_max_packet_size(ByteIOContext *s)
528
{
529
    return s->max_packet_size;
530
}
531

    
532
/* buffer handling */
533
int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags)
534
{
535
    return init_put_byte(s, buf, buf_size, 
536
                         (flags & URL_WRONLY) != 0, NULL, NULL, NULL, NULL);
537
}
538

    
539
/* return the written or read size */
540
int url_close_buf(ByteIOContext *s)
541
{
542
    put_flush_packet(s);
543
    return s->buf_ptr - s->buffer;
544
}
545

    
546
/* output in a dynamic buffer */
547

    
548
typedef struct DynBuffer {
549
    int pos, size, allocated_size;
550
    uint8_t *buffer;
551
    int io_buffer_size;
552
    uint8_t io_buffer[1];
553
} DynBuffer;
554

    
555
static void dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
556
{
557
    DynBuffer *d = opaque;
558
    int new_size, new_allocated_size;
559
    
560
    /* reallocate buffer if needed */
561
    new_size = d->pos + buf_size;
562
    new_allocated_size = d->allocated_size;
563
    while (new_size > new_allocated_size) {
564
        if (!new_allocated_size)
565
            new_allocated_size = new_size;
566
        else
567
            new_allocated_size = (new_allocated_size * 3) / 2 + 1;    
568
    }
569
    
570
    if (new_allocated_size > d->allocated_size) {
571
        d->buffer = av_realloc(d->buffer, new_allocated_size);
572
        if(d->buffer == NULL)
573
             return ;
574
        d->allocated_size = new_allocated_size;
575
    }
576
    memcpy(d->buffer + d->pos, buf, buf_size);
577
    d->pos = new_size;
578
    if (d->pos > d->size)
579
        d->size = d->pos;
580
}
581

    
582
static void dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
583
{
584
    unsigned char buf1[4];
585

    
586
    /* packetized write: output the header */
587
    buf1[0] = (buf_size >> 24);
588
    buf1[1] = (buf_size >> 16);
589
    buf1[2] = (buf_size >> 8);
590
    buf1[3] = (buf_size);
591
    dyn_buf_write(opaque, buf1, 4);
592

    
593
    /* then the data */
594
    dyn_buf_write(opaque, buf, buf_size);
595
}
596

    
597
static int dyn_buf_seek(void *opaque, offset_t offset, int whence)
598
{
599
    DynBuffer *d = opaque;
600

    
601
    if (whence == SEEK_CUR)
602
        offset += d->pos;
603
    else if (whence == SEEK_END)
604
        offset += d->size;
605
    if (offset < 0 || offset > 0x7fffffffLL)
606
        return -1;
607
    d->pos = offset;
608
    return 0;
609
}
610

    
611
static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size)
612
{
613
    DynBuffer *d;
614
    int io_buffer_size, ret;
615
    
616
    if (max_packet_size) 
617
        io_buffer_size = max_packet_size;
618
    else
619
        io_buffer_size = 1024;
620
        
621
    d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
622
    if (!d)
623
        return -1;
624
    d->io_buffer_size = io_buffer_size;
625
    d->buffer = NULL;
626
    d->pos = 0;
627
    d->size = 0;
628
    d->allocated_size = 0;
629
    ret = init_put_byte(s, d->io_buffer, io_buffer_size, 
630
                        1, d, NULL, 
631
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write, 
632
                        max_packet_size ? NULL : dyn_buf_seek);
633
    if (ret == 0) {
634
        s->max_packet_size = max_packet_size;
635
    }
636
    return ret;
637
}
638

    
639
/*
640
 * Open a write only memory stream.
641
 * 
642
 * @param s new IO context
643
 * @return zero if no error.
644
 */
645
int url_open_dyn_buf(ByteIOContext *s)
646
{
647
    return url_open_dyn_buf_internal(s, 0);
648
}
649

    
650
/*
651
 * Open a write only packetized memory stream with a maximum packet
652
 * size of 'max_packet_size'.  The stream is stored in a memory buffer
653
 * with a big endian 4 byte header giving the packet size in bytes.
654
 * 
655
 * @param s new IO context
656
 * @param max_packet_size maximum packet size (must be > 0) 
657
 * @return zero if no error.
658
 */
659
int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size)
660
{
661
    if (max_packet_size <= 0)
662
        return -1;
663
    return url_open_dyn_buf_internal(s, max_packet_size);
664
}
665

    
666
/* 
667
 * Return the written size and a pointer to the buffer. The buffer
668
 *  must be freed with av_free(). 
669
 * @param s IO context
670
 * @param pointer to a byte buffer
671
 * @return the length of the byte buffer
672
 */
673
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
674
{
675
    DynBuffer *d = s->opaque;
676
    int size;
677

    
678
    put_flush_packet(s);
679

    
680
    *pbuffer = d->buffer;
681
    size = d->size;
682
    av_free(d);
683
    return size;
684
}