Revision f5a478f6 libavformat/utils.c

View differences:

libavformat/utils.c
263 263
    return 0;
264 264
}
265 265

  
266
/* fifo handling */
267

  
268
int fifo_init(FifoBuffer *f, int size)
269
{
270
    f->buffer = av_malloc(size);
271
    if (!f->buffer)
272
        return -1;
273
    f->end = f->buffer + size;
274
    f->wptr = f->rptr = f->buffer;
275
    return 0;
276
}
277

  
278
void fifo_free(FifoBuffer *f)
279
{
280
    av_free(f->buffer);
281
}
282

  
283
int fifo_size(FifoBuffer *f, uint8_t *rptr)
284
{
285
    int size;
286

  
287
    if(!rptr)
288
        rptr= f->rptr;
289

  
290
    if (f->wptr >= rptr) {
291
        size = f->wptr - rptr;
292
    } else {
293
        size = (f->end - rptr) + (f->wptr - f->buffer);
294
    }
295
    return size;
296
}
297

  
298
/**
299
 * Get data from the fifo (returns -1 if not enough data).
300
 */
301
int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr)
302
{
303
    uint8_t *rptr;
304
    int size, len;
305

  
306
    if(!rptr_ptr)
307
        rptr_ptr= &f->rptr;
308
    rptr = *rptr_ptr;
309

  
310
    if (f->wptr >= rptr) {
311
        size = f->wptr - rptr;
312
    } else {
313
        size = (f->end - rptr) + (f->wptr - f->buffer);
314
    }
315

  
316
    if (size < buf_size)
317
        return -1;
318
    while (buf_size > 0) {
319
        len = f->end - rptr;
320
        if (len > buf_size)
321
            len = buf_size;
322
        memcpy(buf, rptr, len);
323
        buf += len;
324
        rptr += len;
325
        if (rptr >= f->end)
326
            rptr = f->buffer;
327
        buf_size -= len;
328
    }
329
    *rptr_ptr = rptr;
330
    return 0;
331
}
332

  
333
/**
334
 * Resizes a FIFO.
335
 */
336
void fifo_realloc(FifoBuffer *f, unsigned int new_size){
337
    unsigned int old_size= f->end - f->buffer;
338

  
339
    if(old_size < new_size){
340
        uint8_t *old= f->buffer;
341

  
342
        f->buffer= av_realloc(f->buffer, new_size);
343

  
344
        f->rptr += f->buffer - old;
345
        f->wptr += f->buffer - old;
346

  
347
        if(f->wptr < f->rptr){
348
            memmove(f->rptr + new_size - old_size, f->rptr, f->buffer + old_size - f->rptr);
349
            f->rptr += new_size - old_size;
350
        }
351
        f->end= f->buffer + new_size;
352
    }
353
}
354

  
355
void fifo_write(FifoBuffer *f, const uint8_t *buf, int size, uint8_t **wptr_ptr)
356
{
357
    int len;
358
    uint8_t *wptr;
359

  
360
    if(!wptr_ptr)
361
        wptr_ptr= &f->wptr;
362
    wptr = *wptr_ptr;
363

  
364
    while (size > 0) {
365
        len = f->end - wptr;
366
        if (len > size)
367
            len = size;
368
        memcpy(wptr, buf, len);
369
        wptr += len;
370
        if (wptr >= f->end)
371
            wptr = f->buffer;
372
        buf += len;
373
        size -= len;
374
    }
375
    *wptr_ptr = wptr;
376
}
377

  
378
/* get data from the fifo (return -1 if not enough data) */
379
int put_fifo(ByteIOContext *pb, FifoBuffer *f, int buf_size, uint8_t **rptr_ptr)
380
{
381
    uint8_t *rptr = *rptr_ptr;
382
    int size, len;
383

  
384
    if (f->wptr >= rptr) {
385
        size = f->wptr - rptr;
386
    } else {
387
        size = (f->end - rptr) + (f->wptr - f->buffer);
388
    }
389

  
390
    if (size < buf_size)
391
        return -1;
392
    while (buf_size > 0) {
393
        len = f->end - rptr;
394
        if (len > buf_size)
395
            len = buf_size;
396
        put_buffer(pb, rptr, len);
397
        rptr += len;
398
        if (rptr >= f->end)
399
            rptr = f->buffer;
400
        buf_size -= len;
401
    }
402
    *rptr_ptr = rptr;
403
    return 0;
404
}
405

  
406 266
/**
407 267
 * Allocate the payload of a packet and intialized its fields to default values.
408 268
 *

Also available in: Unified diff