astream.hh
上传用户:aoeyumen
上传日期:2007-01-06
资源大小:3329k
文件大小:3k
源码类别:

DVD

开发平台:

Unix_Linux

  1. /*
  2.    File: bitstr.hh
  3.    By: Alex Theo de Jong
  4.    Created: February 1996
  5. */
  6. #ifndef __astream_hh
  7. #define __astream_hh
  8. #ifdef __GNUG__
  9. #pragma interface
  10. #endif
  11. class AudioStream {
  12.   Mpeg2Buffer* inputstream;
  13.   Synchronization* sync;
  14.   unsigned int    word;           // current bits
  15.   unsigned int    value;          // return value for get_bits
  16.   int       frames;        // counters: bytes left, sum of bits, number of frames read
  17.   int     bitindex;       // number (0-31, from MSB to LSB) of next bit for get_bits()
  18.   bool      file;                 // read from file or buffer
  19.  public:
  20.   int  bytes;
  21.   AudioStream(const char *filename);
  22.   AudioStream(Mpeg2Buffer* input, Synchronization* s);
  23.   ~AudioStream(void);
  24. #ifndef DEBUG
  25.   inline
  26. #endif
  27.   bool get_header(unsigned int&);
  28.   void get_header_ac3(void);
  29.   // get next 32 bits from bitstream in an unsigned int,
  30.   // returned value False => end of stream
  31. #ifndef DEBUG
  32.   inline
  33. #endif
  34.   bool read_frame(int bytesize);
  35.   // fill buffer with data from bitstream, returned value False => end of stream
  36. #ifndef DEBUG
  37.   inline
  38. #endif
  39.   unsigned int get_bits(int number_of_bits);
  40.   // read bits (1 <= number_of_bits <= 16) from buffer into the lower bits
  41.   // of an unsigned int. The LSB contains the latest read bit of the stream.
  42. };
  43. #ifndef DEBUG
  44. // #ifdef NOT
  45. inline unsigned int AudioStream::get_bits(int n){ 
  46.   for (; bytes && bitindex <= 24; bitindex += 8, bytes--)
  47.     word|=(inputstream->getbyte() << (24 - bitindex));
  48.   value=0;
  49.   value=(word >> (32-n));
  50.   bitindex-=n;
  51.   word<<=n;
  52.   return value;
  53. }
  54. inline bool AudioStream::get_header(unsigned int& headerstring){
  55.   if (bytes) inputstream->skipbytes(bytes);
  56.   if (sync) sync->usedbytes(2, 4);  // 2 is audio ID, 4 is number of bytes
  57.   if (inputstream->waitforbytes(4)!=4){
  58.     TRACER("audio stream failed - exit");
  59.     athr_exit(0);
  60.     return False;
  61.   }
  62.   headerstring=inputstream->getbits32();
  63.   inputstream->signal_buffer();
  64.   while (((headerstring & 0xfff00000)!=0xfff00000) ){
  65.     if (inputstream->waitforbytes(1)!=1){
  66.       TRACER("audio stream failed - exit");
  67.       athr_exit(0);
  68.       return False;
  69.     }
  70.     headerstring<<=8;
  71.     headerstring|=(inputstream->getbyte() & 0x000000ff);
  72.     inputstream->signal_buffer();  
  73.   }
  74.   return (headerstring) ? True : False;
  75. }
  76. inline void AudioStream::get_header_ac3(){
  77.   unsigned int headerstring;
  78. //  if (bytes) inputstream->skipbytes(bytes);
  79.   if (sync) sync->usedbytes(2, 2);  // 2 is audio ID, 4 is number of bytes
  80.   if (inputstream->waitforbytes(2)!=2){
  81.     TRACER("audio stream failed - exit");
  82.     athr_exit(0);
  83.     return ;
  84.   }
  85.   headerstring=(inputstream->getbyte() & 0x000000ff);
  86.   headerstring<<=8;
  87.   headerstring|=(inputstream->getbyte() & 0x000000ff);
  88.   inputstream->signal_buffer();
  89.   while (headerstring != 0x0b77 ){
  90.     if (inputstream->waitforbytes(1)!=1){
  91.       TRACER("audio stream failed - exit");
  92.       athr_exit(0);
  93.       return ;
  94.     }
  95.     headerstring&=0x000000ff;
  96.     headerstring<<=8;
  97.     headerstring|=(inputstream->getbyte() & 0x000000ff);
  98.     inputstream->signal_buffer();
  99.   }
  100.   return ;
  101. }
  102. bool AudioStream::read_frame(int bytesize){
  103. int i;
  104.   if ((bytes=inputstream->waitforbytes(bytesize))!=bytesize){
  105.     TRACER("audio stream failed - exit");
  106.     message("audio stream failed - exit");
  107.     athr_exit(0);
  108.     return False;
  109.   }
  110.   if (sync) {
  111.      i = sync->usedbytes(2, bytesize);  // 2 is audio ID, bytesize is number of bytes
  112.   }
  113.   frames++;
  114.   bitindex=0;
  115.   return True;
  116. }
  117. // #endif NOT
  118. #endif DEBUG
  119. #endif  // __astream_hh