diff --git a/libmpdemux/Makefile b/libmpdemux/Makefile index 38933cca35..2beeade9a3 100644 --- a/libmpdemux/Makefile +++ b/libmpdemux/Makefile @@ -3,7 +3,7 @@ LIBNAME = libmpdemux.a include ../config.mak -SRCS = asfheader.c aviheader.c aviprint.c aviwrite.c demux_asf.c demux_avi.c demux_mov.c demux_mpg.c demuxer.c dvdauth.c open.c parse_es.c stream.c +SRCS = cache2.c asfheader.c aviheader.c aviprint.c aviwrite.c demux_asf.c demux_avi.c demux_mov.c demux_mpg.c demuxer.c dvdauth.c open.c parse_es.c stream.c ifeq ($(STREAMING),yes) SRCS += asf_streaming.c url.c http.c network.c endif @@ -25,7 +25,7 @@ $(LIBNAME): $(OBJS) $(AR) r $(LIBNAME) $(OBJS) test: $(LIBNAME) test.c - $(CC) $(CFLAGS) test.c ../mp_msg.c -o test -L. -lmpdemux -ldvdread -lz -lpthread + $(CC) $(CFLAGS) test.c ../mp_msg.c ../linux/shmem.c -o test -L. -lmpdemux -ldvdread -lz -lpthread clean: rm -f *.o *.a *~ diff --git a/libmpdemux/cache2.c b/libmpdemux/cache2.c new file mode 100644 index 0000000000..cc13365716 --- /dev/null +++ b/libmpdemux/cache2.c @@ -0,0 +1,204 @@ +#ifdef USE_STREAM_CACHE +// gcc cache2.c ../linux/shmem.o -o cache2 + +// Initial draft of my new cache system... +// includes some simulation code, using usleep() to emulate limited bandwith +// TODO: seeking, data consistency checking + +#define READ_SPEED 20 +#define FILL_SPEED 10 + +#include +#include +#include +#include + +#include "../linux/shmem.h" + +#include "stream.h" + +int stream_fill_buffer(stream_t *s); +int stream_seek_long(stream_t *s,off_t pos); + + +typedef struct { + // constats: + unsigned char *buffer; // base pointer of the alllocated buffer memory + int buffer_size; // size of the alllocated buffer memory + int sector_size; // size of a single sector (2048/2324) + // Note: buffer_size should be N*sector_size, where N is integer... + int back_size; // we should keep back_size amount of old bytes for backward seek + int fill_limit; // we should fill buffer if space>fill_limit + // reader's pointers: + int read_filepos; + // filler's pointers: + int min_filepos; // buffer contain only a part of the file, from min-max pos + int max_filepos; + int offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte) + // commands/locking: + int cmd_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd + int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads. + // callback + stream_t* stream; +} cache_vars_t; + +int min_fill=0; +int sleep_flag=0; + +void cache_stats(cache_vars_t* s){ + int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer + printf("0x%06X [0x%06X] 0x%06X ",s->min_filepos,s->read_filepos,s->max_filepos); + printf("%3d %% (%3d%%)\n",100*newb/s->buffer_size,100*min_fill/s->buffer_size); +} + +int cache_read(cache_vars_t* s,unsigned char* buf,int size){ + int total=0; + while(size>0){ + int pos,newb,len; + + pos=s->read_filepos - s->offset; + if(pos<0) pos+=s->buffer_size; else + if(pos>=s->buffer_size) pos-=s->buffer_size; + + newb=s->max_filepos-s->read_filepos; // new bytes in the buffer + + if(newbs->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap... + if(newb>size) newb=size; + + // len=write(mem,newb) + //printf("Buffer read: %d bytes\n",newb); + memcpy(buf,&s->buffer[pos],newb); + buf+=newb; + len=newb; //usleep(len*READ_SPEED*sleep_flag); + // ... + + s->read_filepos+=len; + size-=len; + total+=len; + + } + return total; +} + +int cache_fill(cache_vars_t* s){ + int read,back,newb,space,len,pos,endpos; + + read=s->read_filepos; + + // calc number of back-bytes: + back=read - s->min_filepos; + if(back<0) back=0; // strange... + if(back>s->back_size) back=s->back_size; + + // calc number of new bytes: + newb=s->max_filepos - read; + if(newb<0) newb=0; // strange... + + // calc free buffer space: + space=s->buffer_size - (newb+back); + + // calc bufferpos: + pos=s->max_filepos - s->offset; + if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around + + if(spacefill_limit){ +// printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit); + return 0; // no fill... + } + +// printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos); + + // reduce space if needed: + if(space>s->buffer_size-pos) space=s->buffer_size-pos; + +// if(space>32768) space=32768; // limit one-time block size + if(space>4*s->sector_size) space=4*s->sector_size; + + s->min_filepos=read-back; // avoid seeking-back to temp area... + + // .... + //printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size); + //len=space; usleep(len*FILL_SPEED*sleep_flag); + //len=stream_fill_buffer(s->stream); + //memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy! + // .... + stream_read(s->stream,&s->buffer[pos],space); len=space; + + s->max_filepos+=len; + if(pos+len>=s->buffer_size){ + // wrap... + s->offset+=s->buffer_size; + } + + return len; + +} + +cache_vars_t* cache_init(int size,int sector){ + int num; + cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t)); + memset(s,0,sizeof(cache_vars_t)); + num=size/sector; + s->buffer_size=num*sector; + s->sector_size=sector; + s->buffer=shmem_alloc(s->buffer_size); + s->fill_limit=8*sector; + s->back_size=size/2; + return s; +} + +static void exit_sighandler(int x){ + // close stream + exit(0); +} + +void stream_enable_cache(stream_t *s,int size){ + int ss=(s->type==STREAMTYPE_VCD)?VCD_SECTOR_DATA:STREAM_BUFFER_SIZE; + s->cache_data=cache_init(size,ss); + ((cache_vars_t*)s->cache_data)->stream=s; // callback + if((s->cache_pid=fork())) return; // parent exits +// cache thread mainloop: + signal(SIGTERM,exit_sighandler); // kill + while(1){ + if(!cache_fill(s->cache_data)){ + usleep(50000); // idle + } + cache_stats(s->cache_data); + } +} + +int cache_stream_fill_buffer(stream_t *s){ + int len; + if(s->eof){ s->buf_pos=s->buf_len=0; return 0; } + if(!s->cache_pid) return stream_fill_buffer(s); + + len=cache_read(s->cache_data,s->buffer, ((cache_vars_t*)s->cache_data)->sector_size); + + if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; } + s->buf_pos=0; + s->buf_len=len; + s->pos+=len; +// printf("[%d]",len);fflush(stdout); + return len; + +} + +int cache_stream_seek_long(stream_t *s,off_t pos){ + + if(!s->cache_pid) return stream_seek_long(s,pos); + + printf("cache2 seek not implemented!!!!!!!!\n"); + return 0; +} + +#endif diff --git a/libmpdemux/stream.c b/libmpdemux/stream.c index de138cef02..6f6f882c88 100644 --- a/libmpdemux/stream.c +++ b/libmpdemux/stream.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "config.h" #include "mp_msg.h" @@ -176,13 +177,15 @@ stream_t* new_stream(int fd,int type){ s->buf_pos=s->buf_len=0; s->start_pos=s->end_pos=0; s->priv=NULL; + s->cache_pid=0; stream_reset(s); return s; } void free_stream(stream_t *s){ + printf("*** free_stream() called ***\n"); + if(s->cache_pid) kill(s->cache_pid,SIGTERM); if(s->priv) free(s->priv); free(s); } - diff --git a/libmpdemux/stream.h b/libmpdemux/stream.h index 2859b68bc4..72dd21ed4f 100644 --- a/libmpdemux/stream.h +++ b/libmpdemux/stream.h @@ -27,17 +27,27 @@ typedef struct { int type; // 0=file 1=VCD unsigned int buf_pos,buf_len; off_t start_pos,end_pos; + unsigned int cache_pid; + void* cache_data; void* priv; // used for DVD unsigned char buffer[STREAM_BUFFER_SIZE>VCD_SECTOR_SIZE?STREAM_BUFFER_SIZE:VCD_SECTOR_SIZE]; } stream_t; -int stream_fill_buffer(stream_t *s); +#ifdef USE_STREAM_CACHE +void stream_enable_cache(stream_t *s,int size); +#else +// no cache +#define cache_stream_fill_buffer(x) stream_fill_buffer(x) +#define cache_stream_seek_long(x,y) stream_seek_long(x,y) +#define stream_enable_cache(x,y) +#endif -int stream_seek_long(stream_t *s,off_t pos); +int cache_stream_fill_buffer(stream_t *s); +int cache_stream_seek_long(stream_t *s,off_t pos); inline static int stream_read_char(stream_t *s){ return (s->buf_posbuf_len)?s->buffer[s->buf_pos++]: - (stream_fill_buffer(s)?s->buffer[s->buf_pos++]:-256); + (cache_stream_fill_buffer(s)?s->buffer[s->buf_pos++]:-256); // if(s->buf_posbuf_len) return s->buffer[s->buf_pos++]; // stream_fill_buffer(s); // if(s->buf_posbuf_len) return s->buffer[s->buf_pos++]; @@ -81,7 +91,7 @@ inline static void stream_read(stream_t *s,char* mem,int len){ int x; x=s->buf_len-s->buf_pos; if(x==0){ - if(!stream_fill_buffer(s)) return; // EOF + if(!cache_stream_fill_buffer(s)) return; // EOF x=s->buf_len-s->buf_pos; } if(s->buf_pos>s->buf_len) printf("stream_read: WARNING! s->buf_pos>s->buf_len\n"); @@ -112,7 +122,7 @@ inline static int stream_seek(stream_t *s,off_t pos){ } } - return stream_seek_long(s,pos); + return cache_stream_seek_long(s,pos); } inline static int stream_skip(stream_t *s,int len){ @@ -123,7 +133,7 @@ inline static int stream_skip(stream_t *s,int len){ while(len>0){ int x=s->buf_len-s->buf_pos; if(x==0){ - if(!stream_fill_buffer(s)) return 0; // EOF + if(!cache_stream_fill_buffer(s)) return 0; // EOF x=s->buf_len-s->buf_pos; } if(x>len) x=len; diff --git a/libmpdemux/test.c b/libmpdemux/test.c index ee05342281..168c21885b 100644 --- a/libmpdemux/test.c +++ b/libmpdemux/test.c @@ -23,7 +23,7 @@ void skip_audio_frame(sh_audio_t *sh_audio){ void resync_audio_stream(sh_audio_t *sh_audio){ } -int verbose=1; // must be global! +int verbose=5; // must be global! //--------------- @@ -47,9 +47,11 @@ int file_format=DEMUXER_TYPE_UNKNOWN; printf("Cannot open file/device\n"); exit(1); } - + printf("success: format: %d data: 0x%X - 0x%X\n",file_format, (int)(stream->start_pos),(int)(stream->end_pos)); + stream_enable_cache(stream,2048*1024); + demuxer=demux_open(stream,file_format,-1,-1,-1); if(!demuxer){ printf("Cannot open demuxer\n");