00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 int av_fifo_init(AVFifoBuffer *f, int size)
00024 {
00025 f->wptr = f->rptr =
00026 f->buffer = (unsigned char*)av_malloc(size);
00027 f->end = f->buffer + size;
00028 if (!f->buffer)
00029 return -1;
00030 return 0;
00031 }
00032
00033 void av_fifo_free(AVFifoBuffer *f)
00034 {
00035 av_free(f->buffer);
00036 }
00037
00038 int av_fifo_size(AVFifoBuffer *f)
00039 {
00040 int size = f->wptr - f->rptr;
00041 if (size < 0)
00042 size += f->end - f->buffer;
00043 return size;
00044 }
00045
00049 int av_fifo_read(AVFifoBuffer *f, uint8_t *buf, int buf_size)
00050 {
00051 return av_fifo_generic_read(f, buf_size, NULL, buf);
00052 }
00053
00057 void av_fifo_realloc(AVFifoBuffer *f, unsigned int new_size) {
00058 unsigned int old_size= f->end - f->buffer;
00059
00060 if(old_size < new_size){
00061 int len= av_fifo_size(f);
00062 AVFifoBuffer f2;
00063
00064 av_fifo_init(&f2, new_size);
00065 av_fifo_read(f, f2.buffer, len);
00066 f2.wptr += len;
00067 av_free(f->buffer);
00068 *f= f2;
00069 }
00070 }
00071
00072 void av_fifo_write(AVFifoBuffer *f, const uint8_t *buf, int size)
00073 {
00074 do {
00075 int len = FFMIN(f->end - f->wptr, size);
00076 memcpy(f->wptr, buf, len);
00077 f->wptr += len;
00078 if (f->wptr >= f->end)
00079 f->wptr = f->buffer;
00080 buf += len;
00081 size -= len;
00082 } while (size > 0);
00083 }
00084
00085
00087 int av_fifo_generic_read(AVFifoBuffer *f, int buf_size, void (*func)(void*, void*, int), void* dest)
00088 {
00089 int size = av_fifo_size(f);
00090
00091 if (size < buf_size)
00092 return -1;
00093 do {
00094 int len = FFMIN(f->end - f->rptr, buf_size);
00095 if(func) func(dest, f->rptr, len);
00096 else{
00097 memcpy(dest, f->rptr, len);
00098 dest = (uint8_t*)dest + len;
00099 }
00100 av_fifo_drain(f, len);
00101 buf_size -= len;
00102 } while (buf_size > 0);
00103 return 0;
00104 }
00105
00107 void av_fifo_drain(AVFifoBuffer *f, int size)
00108 {
00109 f->rptr += size;
00110 if (f->rptr >= f->end)
00111 f->rptr -= f->end - f->buffer;
00112 }