00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "common.h"
00023 #include "fifo.h"
00024
00025 AVFifoBuffer *av_fifo_alloc(unsigned int size)
00026 {
00027 AVFifoBuffer *f= av_mallocz(sizeof(AVFifoBuffer));
00028 if (!f)
00029 return NULL;
00030 f->buffer = av_malloc(size);
00031 f->end = f->buffer + size;
00032 av_fifo_reset(f);
00033 if (!f->buffer)
00034 av_freep(&f);
00035 return f;
00036 }
00037
00038 void av_fifo_free(AVFifoBuffer *f)
00039 {
00040 if (f) {
00041 av_freep(&f->buffer);
00042 av_free(f);
00043 }
00044 }
00045
00046 void av_fifo_reset(AVFifoBuffer *f)
00047 {
00048 f->wptr = f->rptr = f->buffer;
00049 f->wndx = f->rndx = 0;
00050 }
00051
00052 int av_fifo_size(AVFifoBuffer *f)
00053 {
00054 return (uint32_t)(f->wndx - f->rndx);
00055 }
00056
00057 int av_fifo_space(AVFifoBuffer *f)
00058 {
00059 return f->end - f->buffer - av_fifo_size(f);
00060 }
00061
00062 int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
00063 {
00064 unsigned int old_size = f->end - f->buffer;
00065
00066 if (old_size < new_size) {
00067 int len = av_fifo_size(f);
00068 AVFifoBuffer *f2 = av_fifo_alloc(new_size);
00069
00070 if (!f2)
00071 return AVERROR(ENOMEM);
00072 av_fifo_generic_read(f, f2->buffer, len, NULL);
00073 f2->wptr += len;
00074 f2->wndx += len;
00075 av_free(f->buffer);
00076 *f = *f2;
00077 av_free(f2);
00078 }
00079 return 0;
00080 }
00081
00082 int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
00083 {
00084 unsigned int old_size = f->end - f->buffer;
00085 if(size + (unsigned)av_fifo_size(f) < size)
00086 return AVERROR(EINVAL);
00087
00088 size += av_fifo_size(f);
00089
00090 if (old_size < size)
00091 return av_fifo_realloc2(f, FFMAX(size, 2*size));
00092 return 0;
00093 }
00094
00095
00096 int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int))
00097 {
00098 int total = size;
00099 uint32_t wndx= f->wndx;
00100 uint8_t *wptr= f->wptr;
00101
00102 do {
00103 int len = FFMIN(f->end - wptr, size);
00104 if (func) {
00105 if (func(src, wptr, len) <= 0)
00106 break;
00107 } else {
00108 memcpy(wptr, src, len);
00109 src = (uint8_t*)src + len;
00110 }
00111
00112 wptr += len;
00113 if (wptr >= f->end)
00114 wptr = f->buffer;
00115 wndx += len;
00116 size -= len;
00117 } while (size > 0);
00118 f->wndx= wndx;
00119 f->wptr= wptr;
00120 return total - size;
00121 }
00122
00123
00124 int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int))
00125 {
00126
00127 do {
00128 int len = FFMIN(f->end - f->rptr, buf_size);
00129 if(func) func(dest, f->rptr, len);
00130 else{
00131 memcpy(dest, f->rptr, len);
00132 dest = (uint8_t*)dest + len;
00133 }
00134
00135 av_fifo_drain(f, len);
00136 buf_size -= len;
00137 } while (buf_size > 0);
00138 return 0;
00139 }
00140
00142 void av_fifo_drain(AVFifoBuffer *f, int size)
00143 {
00144 f->rptr += size;
00145 if (f->rptr >= f->end)
00146 f->rptr -= f->end - f->buffer;
00147 f->rndx += size;
00148 }
00149
00150 #ifdef TEST
00151
00152 #undef printf
00153
00154 int main(void)
00155 {
00156
00157 AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
00158 int i, j, n;
00159
00160
00161 for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
00162 av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
00163
00164
00165 n = av_fifo_size(fifo)/sizeof(int);
00166 for (i = -n+1; i < n; i++) {
00167 int *v = (int *)av_fifo_peek2(fifo, i*sizeof(int));
00168 printf("%d: %d\n", i, *v);
00169 }
00170 printf("\n");
00171
00172
00173 for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
00174 av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
00175 printf("%d ", j);
00176 }
00177 printf("\n");
00178
00179 av_fifo_free(fifo);
00180
00181 return 0;
00182 }
00183
00184 #endif