#include "config.h" #include #include #define _LARGEFILE64_SOURCE #include #ifdef HAVE_MALLOC_H #include #else #include #endif #include #include #include #include #include "AVIIndex.h" typedef long long __int64; #define MAXSTREAMS 4 #ifdef __FreeBSD__ #define lseek64 lseek //#else //extern "C" __int64 lseek64(int fd, __int64 offset, int); #endif struct req { __int64 offset; int size; enum status { BUFFER_CLEAN=0, BUFFER_WAITING, BUFFER_WRITING, BUFFER_READY }; status st; Unsigned id;//number of stream Unsigned position;//position in stream char* memory; }; class Cache { Unsigned m_size; Unsigned m_used; AVIIndexEntry2* m_tables[MAXSTREAMS]; Unsigned m_lengths[MAXSTREAMS]; __int64 m_positions[MAXSTREAMS]; pthread_mutex_t mutex_in, mutex_out; pthread_cond_t cond_in, cond_out; pthread_t thread; req* req_buf; int buffers_busy; int m_quit; int cache_access; int cache_right; int cache_miss; enum status { CLEAR, ASYNC }; status m_status; protected: int Prefetch(Unsigned id, Unsigned position); int Update();//removes unused cache entries and requests newer ones public: Cache(Unsigned size); int AddStream(Unsigned id, AVIIndexEntry2 *table, Unsigned position, Unsigned length); ~Cache(); int Clear(); int Create(int fd); int Read(char* buffer, Unsigned id, Unsigned pos, Unsigned size, Unsigned offset=0); }; class InputStream { protected: int m_fd; int m_fd_async; __int64 length; Cache* cache; public: InputStream(int fd, int fd_async); ~InputStream(); offset_t seek(__int64 offset); offset_t seek_cur(__int64 offset); offset_t pos(); offset_t len() const{return length;} int eof(){return (pos()>=length);} int read_int() { int i; read(m_fd, &i, 4); return i; } int Async() //enter asynchronous mode { if(cache==0)return -1; return cache->Create(m_fd_async); } int AddStream(Unsigned id, AVIIndexEntry2 *table, Unsigned position, Unsigned length) { if(cache==0)return -1; return cache->AddStream(id, table, position, length); } // int Read(offset_t offset, char* buffer, Unsigned size);//asynchronous, obsolete int Read(char* buffer, Unsigned size);//synchronous //from cache: int Read(char* buffer, Unsigned id, Unsigned pos, Unsigned size, Unsigned offset =0) { if(cache==0)return -1; return cache->Read(buffer, id, pos, size, offset); } int Clear() { if(cache==0)return -1; return cache->Clear(); } };