www.pudn.com > mediator15src.zip > asyncstream.h


/* 
 * asyncstream.h 
 * Copyright (C) 2001-2002 Arno Hornberger  
 * 
 * This file is part of MPEG Mediator, a free MPEG stream converter. 
 * 
 * MPEG Mediator is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version. 
 * 
 * MPEG Mediator is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */ 
 
#ifndef ASYNCSTREAM_H 
#define ASYNCSTREAM_H 
 
#include  
#include  
#include  
#include "demuxexcept.h" 
 
class AsyncStream 
{ 
  private: 
		enum 
		{ 
			BUFFERSIZE = 0x80000,    // 524288 bytes, 512 KByte 
      BUFFERMASK = 0x7ffff, 
			CHUNKSIZE = 16384,	     // 16 KByte 
			MAXFILES = 16 
		}; 
 
		int filehandles[MAXFILES]; 
//		FILE *filehandles[MAXFILES]; 
    __int64 filesizes[MAXFILES]; 
    int nfiles; 
 
    unsigned char *bitbuffer; 
    __int64 start, stop; 
    __int64 pos; 
    int pos_bit; 
    bool end_of_stream; 
 
    int ReadChunk(); 
    int ReadBytes(unsigned char *buffer, int size, __int64 pos); 
    int WhichFile(__int64 pos); 
    void CopyBlock(unsigned char *dest, int nbytes); 
 
  public: 
    AsyncStream(); 
    ~AsyncStream(); 
 
    void Open(char **filenames, int count); 
    void Close(); 
 
    __int64 Size(); 
 
    void Seek(__int64 pos); 
    __int64 Tell() { return pos + 1; } 
 
    void Align() { pos_bit = 0; } 
 
    unsigned long GetBits(int nbits); 
    unsigned long GetBit(); 
    void FlushBit(); 
    unsigned char GetByte(); 
    void FlushByte(); 
    void GetBytes(unsigned char *dest, int nbytes); 
    void FlushBytes(int nbytes); 
    unsigned long LookAhead(int nbits); 
}; 
 
inline unsigned char AsyncStream::GetByte() 
{ 
  pos++; 
  pos_bit = 0; 
  if ((int)pos == (int)stop) { 
    if (end_of_stream) 
      throw DemuxExcept(DemuxExcept::END_OF_STREAM); 
    if (ReadChunk() == 0)  
      throw DemuxExcept(DemuxExcept::END_OF_STREAM); 
  } 
  return bitbuffer[(int)pos & BUFFERMASK]; 
} 
 
inline void AsyncStream::FlushByte() 
{ 
  pos++; 
  pos_bit = 0; 
  if ((int)pos == (int)stop) { 
    if (end_of_stream) 
      throw DemuxExcept(DemuxExcept::END_OF_STREAM); 
    if (ReadChunk() == 0)  
      throw DemuxExcept(DemuxExcept::END_OF_STREAM); 
  } 
} 
 
inline void AsyncStream::FlushBit() 
{ 
  if (pos_bit == 0) { 
    pos++; 
    if ((int)pos == (int)stop) { 
      if (end_of_stream) 
        throw DemuxExcept(DemuxExcept::END_OF_STREAM); 
      if (ReadChunk() == 0)  
        throw DemuxExcept(DemuxExcept::END_OF_STREAM); 
    } 
    pos_bit = 8; 
  } 
  pos_bit--; 
} 
 
inline int AsyncStream::WhichFile(__int64 pos) 
{ 
  int i = 0; 
   
  while (i < nfiles) { 
    if (pos < filesizes[i]) 
      break; 
    pos -= filesizes[i++]; 
  } 
  return (i == nfiles) ? (i - 1) : i; 
} 
 
#endif