home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Demux / Demux.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  8.3 KB  |  280 lines

  1. /* 
  2.  *  demux.h 
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #ifndef DEMUX_H
  25. #define DEMUX_H
  26.  
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <math.h>
  31. #include <windows.h>
  32. #include <winbase.h>
  33.  
  34. #include "..\mism.h"
  35.  
  36.  
  37. #define IDCT_MMX    1
  38. #define IDCT_NORMAL    2
  39. #define IDCT_IEEE    3
  40.  
  41. #define MPEG1_CLK_REF  (i64)90000
  42. #define MPEG2_CLK_REF  (i64)27000000
  43.  
  44.  
  45. // Stream IDs
  46. #define PACK_ID                     0xBA
  47. #define PROGRAM_STREAM_MAP       0xBC
  48. #define PRIVATE_STREAM_1         0xBD
  49. #define PADDING_STREAM           0xBE
  50. #define PRIVATE_STREAM_2         0xBF
  51. #define ECM_STREAM               0xF0
  52. #define EMM_STREAM               0xF1
  53. #define PROGRAM_STREAM_DIRECTORY 0xFF
  54. #define DSMCC_STREAM             0xF2
  55. #define ITUTRECH222TYPEE_STREAM  0xF8
  56. #define SUBSTREAM_AC3            0x80
  57. #define VIDEO_STREAM             0xE0
  58. #define AUDIO_STREAM             0xC0
  59.  
  60.  
  61. // PES definition
  62. struct PESinfo
  63. {
  64.  
  65. bool  pack_header_parsed; // flag that indicates if the PES was the first in the Pack
  66. unsigned int  pack_bytes; // number of bytes inside the Pack from the byte containing
  67.                           // the last bit of SCRbase field
  68.  
  69. int   streamID;          // streamID of the PES
  70. int      subStreamID;      // subStreamID of the PES
  71.  
  72.                       // Pack info
  73. __int64   SCR;            // System Clock Reference referred to a 27Mhz clock
  74. int    muxrate;        // Pack muxrate in bytes per second
  75.  
  76.                       // PES info
  77. __int64   PTS;            // Presentation Time Stamp referred to a 27Mhz clock
  78. unsigned int payloadSize;    // size in bytes of data contained inside the PES packet
  79. bool  isValidData;     // flag to specify if the data returned is valid or not
  80. };
  81.  
  82.  
  83. // MPEG2 PACK HEADER
  84. // SCRbase and SCRext
  85. //                     32 .......................................0 9.........0
  86. // [PACK_START_CODE][01---1--][--------][-----1--][--------][-----1--][-------1]
  87.  
  88. #define ZERO            (i64)0
  89. #define GET_SCRBASE(x) ( ((ui64)x[4]&0xF8)>>3  | \
  90.                           (ui64)x[3]<<5        | \
  91.                          ((ui64)x[2]&0x03)<<13 | \
  92.                          ((ui64)x[2]&0xF8)<<12 | \
  93.                           (ui64)x[1]<<20       | \
  94.                          ((ui64)x[0]&0x03)<<28 | \
  95.                          ((ui64)x[0]&0x38) <<27 )
  96.  
  97. #define GET_SCREXT(x)  ( ((ui64)x[5]&0xFE)>>1     | \
  98.                          ((ui64)x[4]&0x03)<<7 )
  99.  
  100. // muxrate
  101. //      22 ......................0         stl
  102. // ... [--------][--------][------11][rrrrr---]
  103. #define GET_MPEG2MUXRATE(x) ( (ui32)x[6]<<14 | ((ui32)x[7])<<6 | ((ui32)x[8]&0x03)>>2)
  104.  
  105. #define GET_MPEG2STUFFING(x) ((x[9]&0x07)) 
  106. // MPEG1 PACK HEADER
  107. //                       SCR                                            muxrate
  108. //                       32........................................0    22.......................0
  109. // [PACK_START_CODE][0010---1][--------][-------1][--------][-------1][1-------][--------][-------1]
  110. #define GET_SCR(x)     ( ((ui64)x[4]&0xFE) >>1     | \
  111.                          ((ui64)x[3])      <<7     | \
  112.                          ((ui64)x[2]&0xFE) <<14    | \
  113.                          ((ui64)x[1])      <<22    | \
  114.                          ((ui64)x[0]&0x0E) <<29 )
  115. #define GET_MPEG1_PTS(x) (GET_SCR(x)) //they follow the same pattern
  116.  
  117. #define GET_MPEG1_MUXRATE(x) ( ((ui32)x[7]&0xFE) >>1     | \
  118.                                ((ui32)x[6])         <<7     | \
  119.                                ((ui32)x[5]&0x7F) <<15 )
  120.  
  121. // MPEG2 PES packet (first 5 bytes)
  122. //                     Packet lenght                 PTS       Header length
  123. //                     15...............0                      7......0            
  124. // [PACKET_START_CODE][--------][--------][10------][xx------][--------]
  125. #define GET_MPEG_PACKET_LENGHT(x) ( ((ui16)x[0]<<8) | (ui16)x[1] )
  126. #define GET_MPEG2_PTS_FLAGS(x) ( ((ui8)x[3]&0xC0)>>6  )
  127. // MPEG2 PES packet (optional parameters)
  128. //                         PTS                      
  129. //                         32........................................0
  130. // [PACKET_START_CODE][001x---1][--------][-------1][--------][-------1]
  131. #define GET_MPEG2_PTS(x)   ( ((ui64)x[4]&0xFE) >>1     | \
  132.                                ((ui64)x[3])      <<7     | \
  133.                              ((ui64)x[2]&0xFE) <<14    | \
  134.                              ((ui64)x[1])      <<22    | \
  135.                              ((ui64)x[0]&0x0E) <<29 )
  136.  
  137.  
  138. #define IS_MPEG2PACKHEADER(x) (    ((x[0]&0xC4)==0x44) && ((x[2]&0x04)==0x04) \
  139. && ((x[4]&0x04)==0x04) &&  (x[5]&0x01)        )
  140.  
  141. #define IS_MPEG1PACKHEADER(x) (    ((x[0]&0xF1)==0x21) && (x[2]&0x01) \
  142. &&  (x[4]&0x01) )
  143.  
  144. #define GET_UINT16(x)   ( ((ui16)x[0]<<8)|((ui16)x[1]))
  145.  
  146. #define POS_ENG_SIZE 2048 
  147.  
  148. class CDemux
  149. {
  150. public:
  151.  
  152.     // Rewinds the stream to the beginning of the previous
  153.     // PES
  154.     bool RewindPreviousPES( ui8 nStreamID );
  155.     void StartReadLPES();
  156.     bool ReadLPES(unsigned char **buffer, PESinfo *PES);
  157.     i64 GetTime();
  158.  
  159.     CDemux();
  160.     ~CDemux();
  161.  
  162.     bool ReadPES(unsigned char **buffer, PESinfo *PES);
  163.  
  164.     void ResetSCR();
  165.     void ResetPTS();
  166.  
  167.   bool SetStreamPos(ui64 pos);
  168.   ui64 GetStreamPos();
  169.   char *GetFileName();
  170.   ui64 GetStreamSize();
  171.  
  172.   // Retrieve the beginning of the
  173.   // pack that best closes this position
  174.   ui64 GetSyncPoint(ui64 pos);
  175.  
  176.     i64 getSCR();
  177.     i64    getPTS();
  178.  
  179.     int         isMPEG2;
  180.     ui32        muxRate;
  181.  
  182.  
  183.     bool        PTSread;
  184.     int        SetInput(LPTWorkingMism pMismInfo);
  185.  
  186.     bool                EndOfFile;
  187.  
  188. protected:
  189.     ui8    inbuf[65536];
  190.     i64     SCR, SCRbase,SCRext, PTS;
  191.     int     AlignPack();
  192.  
  193.  
  194. private:
  195.   fmHandle strHandle;
  196.   int (*mismReadStream)(fmHandle strHandle, ui8 *buf, unsigned int size); 
  197.   int ReadStream(ui8 *buf, unsigned int size);
  198.   LPTWorkingMism inp;
  199.     bool firstTime;
  200.      i64 delta;
  201.      i64 lastSCR;
  202.     ui32 lastPackBytes;
  203.     ui32 lastMuxRate;
  204.     ui32 pack_bytes;
  205.  
  206.     // Private methods
  207.     bool GetBackByte( ui8 *byte );
  208.     bool GetFordByte( ui8 *byte );
  209.   bool GetFordDWord( ui32 *dword );    
  210.     bool GetForWord( ui16 *word );
  211.  
  212.   // Performance-wise functions
  213.   // to seek the stream backwards and fordward.
  214.  
  215.   // Creates the positioning buffer
  216.   // And positions the pointers from the current position
  217.   void StartPositioningEngine()
  218.   {
  219.     m_pPosEngBfr = new ui8[POS_ENG_SIZE];
  220.     // Get position
  221.     ui64 nActualPos = GetStreamPos();
  222.     SetPosEngPos( nActualPos );
  223.   };
  224.   void StopPositioningEngine()
  225.   {
  226.     // Seek the stream to the actual position
  227.     SetStreamPos( m_nBasePos + m_nPosEngPtr );
  228.     // Delete buffer
  229.     delete []m_pPosEngBfr;
  230.   }
  231.   // Grabs previous or next chunk of data.
  232.   bool GrabPrevChunk() // positions the pointer at the end
  233.   {
  234.     if( ((i64)m_nBasePos - POS_ENG_SIZE)  < 0 )
  235.       return false;
  236.     
  237.     SetStreamPos( m_nBasePos - POS_ENG_SIZE );
  238.     m_nBasePos -= POS_ENG_SIZE;
  239.     m_nPosEngSize = ReadStream( m_pPosEngBfr, POS_ENG_SIZE );    
  240.     m_nPosEngPtr  = m_nPosEngSize;
  241.     return true;
  242.   }
  243.   bool GrabNextChunk() // positions the pointer at the beginning
  244.   {
  245.     if( (m_nBasePos + POS_ENG_SIZE)  > GetStreamSize() )
  246.       return false;
  247.     
  248.     SetStreamPos( m_nBasePos + POS_ENG_SIZE );
  249.     m_nBasePos += POS_ENG_SIZE;
  250.     m_nPosEngSize = ReadStream( m_pPosEngBfr, POS_ENG_SIZE );    
  251.     m_nPosEngPtr  = 0;
  252.     return true;
  253.   }
  254.  
  255.   ui64 GetPosEngPos(){
  256.     return m_nBasePos + m_nPosEngPtr;
  257.   }
  258.   bool SetPosEngPos(ui64 pos){
  259.     m_nPosEngPtr = (i32)(pos % POS_ENG_SIZE);
  260.     m_nBasePos   = pos - m_nPosEngPtr;
  261.     // Now, read the chunk
  262.     // First position the stream at the base
  263.     SetStreamPos( m_nBasePos );
  264.     m_nPosEngSize = ReadStream( m_pPosEngBfr, POS_ENG_SIZE );
  265.     return true;
  266.   };
  267.   // Properties
  268.   ui8 *m_pPosEngBfr;
  269.    i32 m_nPosEngPtr;
  270.   ui32 m_nPosEngSize;
  271.   ui64 m_nBasePos;
  272.  
  273.     
  274.     
  275. };
  276.  
  277.  
  278.  
  279.  
  280. #endif  DEMUX_H