() 10l
[libav.git] / libavutil / fifo.c
CommitLineData
f5a478f6
RS
1/*
2 * A very simple circular buffer FIFO implementation
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2006 Roman Shaposhnik
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20#include "common.h"
21#include "fifo.h"
22
23int av_fifo_init(AVFifoBuffer *f, int size)
24{
25 f->buffer = av_malloc(size);
26 if (!f->buffer)
27 return -1;
28 f->end = f->buffer + size;
29 f->wptr = f->rptr = f->buffer;
30 return 0;
31}
32
33void av_fifo_free(AVFifoBuffer *f)
34{
35 av_free(f->buffer);
36}
37
38int av_fifo_size(AVFifoBuffer *f)
39{
40 int size = f->wptr - f->rptr;
41 if (size < 0)
42 size += f->end - f->buffer;
43 return size;
44}
45
46/**
47 * Get data from the fifo (returns -1 if not enough data).
48 */
49int av_fifo_read(AVFifoBuffer *f, uint8_t *buf, int buf_size)
50{
51 int len;
52 int size = f->wptr - f->rptr;
53 if (size < 0)
54 size += f->end - f->buffer;
55
56 if (size < buf_size)
57 return -1;
58 while (buf_size > 0) {
59 len = FFMIN(f->end - f->rptr, buf_size);
60 memcpy(buf, f->rptr, len);
61 buf += len;
62 f->rptr += len;
63 if (f->rptr >= f->end)
64 f->rptr = f->buffer;
65 buf_size -= len;
66 }
67 return 0;
68}
69
70/**
71 * Resizes a FIFO.
72 */
73void av_fifo_realloc(AVFifoBuffer *f, unsigned int new_size) {
74 unsigned int old_size= f->end - f->buffer;
75
76 if(old_size < new_size){
77 uint8_t *old= f->buffer;
78
79 f->buffer= av_realloc(f->buffer, new_size);
80
81 f->rptr += f->buffer - old;
82 f->wptr += f->buffer - old;
83
84 if(f->wptr < f->rptr){
85 memmove(f->rptr + new_size - old_size, f->rptr, f->buffer + old_size - f->rptr);
86 f->rptr += new_size - old_size;
87 }
88 f->end= f->buffer + new_size;
89 }
90}
91
92void av_fifo_write(AVFifoBuffer *f, const uint8_t *buf, int size)
93{
94 int len;
95
96 while (size > 0) {
97 len = FFMIN(f->end - f->wptr, size);
98 memcpy(f->wptr, buf, len);
99 f->wptr += len;
100 if (f->wptr >= f->end)
101 f->wptr = f->buffer;
102 buf += len;
103 size -= len;
104 }
105}
106
107
108/* get data from the fifo (return -1 if not enough data) */
109int av_fifo_generic_read(AVFifoBuffer *f, int buf_size, void (*func)(void*, void*, int), void* dest)
110{
111 int len;
112 int size = f->wptr - f->rptr;
113 if (size < 0)
114 size += f->end - f->buffer;
115
116 if (size < buf_size)
117 return -1;
118 while (buf_size > 0) {
119 len = FFMIN(f->end - f->rptr, buf_size);
120 func(dest, f->rptr, len);
121 f->rptr += len;
122 if (f->rptr >= f->end)
123 f->rptr = f->buffer;
124 buf_size -= len;
125 }
126 return 0;
127}
128
129/* discard data from the fifo */
130void av_fifo_drain(AVFifoBuffer *f, int size)
131{
132 f->rptr += size;
133 if (f->rptr >= f->end)
134 f->rptr -= f->end - f->buffer;
135}