• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavutil/fifo.c

Go to the documentation of this file.
00001 /*
00002  * a very simple circular buffer FIFO implementation
00003  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
00004  * Copyright (c) 2006 Roman Shaposhnik
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 // src must NOT be const as it can be a context for func that may need updating (like a pointer or byte counter)
00083 int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int))
00084 {
00085     int total = size;
00086     uint32_t wndx= f->wndx;
00087     uint8_t *wptr= f->wptr;
00088 
00089     do {
00090         int len = FFMIN(f->end - wptr, size);
00091         if (func) {
00092             if (func(src, wptr, len) <= 0)
00093                 break;
00094         } else {
00095             memcpy(wptr, src, len);
00096             src = (uint8_t*)src + len;
00097         }
00098 // Write memory barrier needed for SMP here in theory
00099         wptr += len;
00100         if (wptr >= f->end)
00101             wptr = f->buffer;
00102         wndx += len;
00103         size -= len;
00104     } while (size > 0);
00105     f->wndx= wndx;
00106     f->wptr= wptr;
00107     return total - size;
00108 }
00109 
00110 
00111 int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int))
00112 {
00113 // Read memory barrier needed for SMP here in theory
00114     do {
00115         int len = FFMIN(f->end - f->rptr, buf_size);
00116         if(func) func(dest, f->rptr, len);
00117         else{
00118             memcpy(dest, f->rptr, len);
00119             dest = (uint8_t*)dest + len;
00120         }
00121 // memory barrier needed for SMP here in theory
00122         av_fifo_drain(f, len);
00123         buf_size -= len;
00124     } while (buf_size > 0);
00125     return 0;
00126 }
00127 
00129 void av_fifo_drain(AVFifoBuffer *f, int size)
00130 {
00131     f->rptr += size;
00132     if (f->rptr >= f->end)
00133         f->rptr -= f->end - f->buffer;
00134     f->rndx += size;
00135 }
00136 
00137 #ifdef TEST
00138 
00139 #undef printf
00140 
00141 int main(void)
00142 {
00143     /* create a FIFO buffer */
00144     AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
00145     int i, j, n;
00146 
00147     /* fill data */
00148     for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
00149         av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
00150 
00151     /* peek at FIFO */
00152     n = av_fifo_size(fifo)/sizeof(int);
00153     for (i = -n+1; i < n; i++) {
00154         int *v = (int *)av_fifo_peek2(fifo, i*sizeof(int));
00155         printf("%d: %d\n", i, *v);
00156     }
00157     printf("\n");
00158 
00159     /* read data */
00160     for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
00161         av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
00162         printf("%d ", j);
00163     }
00164     printf("\n");
00165 
00166     av_fifo_free(fifo);
00167 
00168     return 0;
00169 }
00170 
00171 #endif
Generated on Fri Feb 1 2013 14:34:56 for FFmpeg by doxygen 1.7.1