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 int av_fifo_init(AVFifoBuffer *f, int size)
00026 {
00027 f->wptr = f->rptr =
00028 f->buffer = av_malloc(size);
00029 f->end = f->buffer + size;
00030 if (!f->buffer)
00031 return -1;
00032 return 0;
00033 }
00034
00035 void av_fifo_free(AVFifoBuffer *f)
00036 {
00037 av_free(f->buffer);
00038 }
00039
00040 int av_fifo_size(AVFifoBuffer *f)
00041 {
00042 int size = f->wptr - f->rptr;
00043 if (size < 0)
00044 size += f->end - f->buffer;
00045 return size;
00046 }
00047
00051 int av_fifo_read(AVFifoBuffer *f, uint8_t *buf, int buf_size)
00052 {
00053 return av_fifo_generic_read(f, buf_size, NULL, buf);
00054 }
00055
00059 void av_fifo_realloc(AVFifoBuffer *f, unsigned int new_size) {
00060 unsigned int old_size= f->end - f->buffer;
00061
00062 if(old_size < new_size){
00063 int len= av_fifo_size(f);
00064 AVFifoBuffer f2;
00065
00066 av_fifo_init(&f2, new_size);
00067 av_fifo_read(f, f2.buffer, len);
00068 f2.wptr += len;
00069 av_free(f->buffer);
00070 *f= f2;
00071 }
00072 }
00073
00074 void av_fifo_write(AVFifoBuffer *f, const uint8_t *buf, int size)
00075 {
00076 do {
00077 int len = FFMIN(f->end - f->wptr, size);
00078 memcpy(f->wptr, buf, len);
00079 f->wptr += len;
00080 if (f->wptr >= f->end)
00081 f->wptr = f->buffer;
00082 buf += len;
00083 size -= len;
00084 } while (size > 0);
00085 }
00086
00087
00089 int av_fifo_generic_read(AVFifoBuffer *f, int buf_size, void (*func)(void*, void*, int), void* dest)
00090 {
00091 int size = av_fifo_size(f);
00092
00093 if (size < buf_size)
00094 return -1;
00095 do {
00096 int len = FFMIN(f->end - f->rptr, buf_size);
00097 if(func) func(dest, f->rptr, len);
00098 else{
00099 memcpy(dest, f->rptr, len);
00100 dest = (uint8_t*)dest + len;
00101 }
00102 av_fifo_drain(f, len);
00103 buf_size -= len;
00104 } while (buf_size > 0);
00105 return 0;
00106 }
00107
00109 void av_fifo_drain(AVFifoBuffer *f, int size)
00110 {
00111 f->rptr += size;
00112 if (f->rptr >= f->end)
00113 f->rptr -= f->end - f->buffer;
00114 }