home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / mpgvideo.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  9.6 KB  |  299 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11. //
  12. /******************************Module*Header*******************************\
  13. * Module Name: MpgVideo.h
  14. *
  15. * Prototype Mpeg Video codec
  16. *
  17. \**************************************************************************/
  18. #ifndef _INC_MPGVIDEO_H
  19. #define _INC_MPGVIDEO_H
  20. #include <streams.h>
  21. #include <windowsx.h>
  22. #include <mmsystem.h>
  23. #include <mmreg.h>
  24. #include <stddef.h>
  25. #include <string.h>
  26.  
  27. #include "Decoder.h"
  28.  
  29.  
  30. // -------------------------------------------------------------------------
  31. // Helper functions that can be used by audio and video codecs.
  32. // -------------------------------------------------------------------------
  33. //
  34.  
  35. /******************************Public*Routine******************************\
  36. * ByteSwap
  37. *
  38. * Converts dwX from little endian to big endian and vice-versa.
  39. *
  40. \**************************************************************************/
  41. __inline DWORD
  42. ByteSwap(
  43.     DWORD dwX
  44.     )
  45. {
  46. #ifdef _X86_
  47.     _asm    mov     eax, dwX
  48.     _asm    bswap   eax
  49.     _asm    mov     dwX, eax
  50.  
  51.     return dwX;
  52. #else
  53.     return _lrotl(((dwX & 0xFF00FF00) >> 8) | ((dwX & 0x00FF00FF) << 8), 16);
  54. #endif
  55. }
  56.  
  57. LPBYTE SkipToPacketData(LPBYTE pSrc, long &LenLeftInPacket);
  58.  
  59.  
  60.  
  61. // -------------------------------------------------------------------------
  62. // This structure is used to associate Pts time stamps with positions
  63. // within the circular buffer.  Each time we get a media sample with a Pts
  64. // time stamp we add a TIMEPOSITION entry to a queue of time positions.
  65. // Each time we decode an I frame we record the starting and ending position
  66. // of the I frame picture within the circular buffer, this information is
  67. // then used to find a suitable time stamp to associate with the frame.  If
  68. // a suitable time stamp cannot be found or the frame is not an I frame we
  69. // calculate a suitable time code by extrapolation.
  70. // -------------------------------------------------------------------------
  71. //
  72. class CTimePosition {
  73. public:
  74.     CRefTime    m_PtsTimeStamp;
  75.     LPBYTE      m_BufferPosition;
  76.  
  77.     CTimePosition(CRefTime *TimeStamp, LPBYTE lpPos)
  78.         : m_PtsTimeStamp(*TimeStamp), m_BufferPosition(lpPos) {}
  79. };
  80.  
  81.  
  82. // -------------------------------------------------------------------------
  83. // Structure to hold the contents of an Mpeg 1 sequence header.
  84. // -------------------------------------------------------------------------
  85. struct SEQHDR_INFO {
  86.     LONG           lWidth;             //  Native Width in pixels
  87.     LONG           lHeight;            //  Native Height in pixels
  88.     LONG           lvbv;               //  vbv
  89.     REFERENCE_TIME tPictureTime;       //  Time per picture in 100ns units
  90.     LONG           lTimePerFrame;      //  Time per picture in MPEG units
  91.     LONG           dwBitRate;          //  Bits per second
  92.     LONG           lXPelsPerMeter;     //  Pel aspect ratio
  93.     LONG           lYPelsPerMeter;     //  Pel aspect ratio
  94.     DWORD          dwStartTimeCode;    //  First GOP time code (or -1)
  95.     LONG           lActualHeaderLen;   //  Length of valid bytes in raw seq hdr
  96.     BYTE           RawHeader[140];     //  The real sequence header
  97. };
  98.  
  99.  
  100. // -------------------------------------------------------------------------
  101. // Quartz Mpeg video codec framework class
  102. //
  103. // -------------------------------------------------------------------------
  104. //
  105. class CMpegVideoOutputPin;
  106. class CMpegVideoInpuPin;
  107.  
  108. class CMpegVideoCodec
  109.     : public CTransformFilter {
  110.  
  111. public:
  112.  
  113.     //
  114.     // --- Com stuff ---
  115.     //
  116.     static CUnknown *CreateInstance(LPUNKNOWN, HRESULT *);
  117.     STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv);
  118.     DECLARE_IUNKNOWN;
  119.  
  120.     //
  121.     // --- CTransform overrides ---
  122.     //
  123.     HRESULT Receive(IMediaSample *pSample);
  124.     HRESULT CheckInputType(const CMediaType* mtIn);
  125.     HRESULT CheckTransform(const CMediaType* mtIn, const CMediaType* mtOut);
  126.     HRESULT DecideBufferSize(IMemAllocator * pAllocator,
  127.                              ALLOCATOR_PROPERTIES * pProperties);
  128.     HRESULT SetMediaType(PIN_DIRECTION direction,const CMediaType *pmt);
  129.     HRESULT GetMediaType(int iPosition, CMediaType *pMediaType);
  130.     HRESULT StartStreaming(void);
  131.     HRESULT StopStreaming(void);
  132.     HRESULT EndOfStream(void);
  133.     HRESULT EndFlush(void);
  134.     HRESULT AlterQuality(Quality q);
  135.  
  136.  
  137.     CMpegVideoCodec(TCHAR *pName, LPUNKNOWN pUnk, HRESULT *pHr);
  138.     ~CMpegVideoCodec();
  139.  
  140.  
  141.     //
  142.     // Callback function from low-level deocoder to get an output
  143.     // buffer to draw into.
  144.     //
  145.     LPBYTE GetDecodeBufferAndFormat();
  146.  
  147.     // setup helper
  148.     LPAMOVIESETUP_FILTER GetSetupData();
  149.  
  150. private:
  151.     LPBYTE          m_pSample;
  152.     LONG            m_LenLeftInPacket;
  153.  
  154.     //
  155.     // Input buffer
  156.     //
  157.     int             m_VBlockSize;
  158.     LPBYTE          m_Buffer;
  159.     LPBYTE          m_BufferStart;
  160.     LPBYTE          m_BufferCurr;
  161.     LONG            m_BufferFullness;
  162.     void            UpdateBufferPosition(LPBYTE lpNewCurrent);
  163.  
  164.  
  165.     //
  166.     // Output buffer, for decode I and P frames.
  167.     //
  168.     LPBYTE          m_pFrameBuff;
  169.  
  170.  
  171.     //
  172.     // Sequence header of the video stream beging played.
  173.     //
  174.     SEQHDR_INFO     m_seqInfo;
  175.  
  176.  
  177.  
  178.     //
  179.     // Decode statistics, number of I, P, B and skipped frames.
  180.     //
  181.     DWORD           m_dwFramesDecoded[4];
  182.     DWORD           m_dwFramesSkipped[4];
  183.     int             m_PerfDecode;
  184.     int             m_QualMsg;
  185.     int             m_FrameDrawn;
  186.     int             m_FrameType;
  187.  
  188.  
  189.     //
  190.     // This is the low level mpeg video decoder that all the work
  191.     //
  192.     DWORD           m_dwLateBy;
  193.     DWORD           m_dwCtrl;
  194.     DWORD           m_dwCtrlDefault;
  195.     DWORD           m_dwQualDefault;
  196.     CVideoDecoder   *m_pVideoDecoder;
  197.     VideoCtrl       m_VideoControl;
  198.  
  199.     //
  200.     // The decoder calls back to get the colour conversion format
  201.     // and a buffer to decode into.  The callback is static because it
  202.     // does not have a "this" pointer.  hrCallback is used to keep a record
  203.     // of any error that may have occurred during the callback.
  204.     //
  205.     void            SetOutputPinMediaType(const CMediaType *pmt);
  206.     IMediaSample    *m_pOutSample;
  207.     HRESULT         hrCallback;
  208.  
  209.  
  210.  
  211.     //
  212.     // If the input sample contains a sync point then its
  213.     // sample time is correct.  I will use this time to resync my
  214.     // internal clock and reset the count of frames decoded
  215.     // since the previous sync point.  Otherwise, if the sample
  216.     // is not a sync point I use the average time per frame and
  217.     // the count of frames since the last sync point to interpolate
  218.     // a suitable time for the newly decoded frame.
  219.     //
  220.     CRefTime        m_TimeAtLastSyncPoint;
  221.     CRefTime        m_TimeSinceLastSyncPoint;
  222.     CRefTime        m_tStopPrev;     // Previous stop time
  223.     CGenericList<CTimePosition> m_PtsQueue;
  224.  
  225.  
  226.     //
  227.     // IP Frame tracking class
  228.     //
  229.     class CNextIP
  230.     {
  231.     public:
  232.         CNextIP();
  233.         BOOL GotFirst();
  234.         void NextRef(DWORD dwTemporalReference);
  235.         BOOL GetTime(REFERENCE_TIME *pt);
  236.         BOOL TimeToDraw() const;
  237.         void Reset();
  238.         void Set(DWORD dwSkipFlag, BOOL bIFrame,
  239.                  BOOL bTimeSet, REFERENCE_TIME t, DWORD dwTemporalReference);
  240.  
  241.     private:
  242.         DWORD           m_dwSkipFlag;
  243.         BOOL            m_bGotFirst;
  244.         BOOL            m_bTimeSet;
  245.         BOOL            m_bTimeToDraw;
  246.         REFERENCE_TIME  m_t;
  247.         DWORD           m_dwTemporalReference;
  248.     };
  249.  
  250.     CNextIP         m_NextIP;
  251.  
  252.  
  253.     //
  254.     // Remember whether the media type specified packets or payload
  255.     //
  256.     BOOL            m_bPayloadOnly;
  257.  
  258.  
  259.     //
  260.     // On 8 bit displays the user can select a gray scale palette
  261.     // alteranative to the default colour palette, this bypasses the
  262.     // colour space conversion code.
  263.     //
  264.     enum MEDIA_FORMATS {MM_411PK, MM_422PK, MM_422SPK, MM_RGB24_DIB,
  265.                         MM_RGB24_DDB, MM_RGB565_DIB, MM_RGB565_DDB,
  266.                         MM_RGB555_DIB, MM_RGB555_DDB, MM_RGB8_DIB,
  267.                         MM_RGB8_DDB, MM_Y_DIB, MM_Y_DDB};
  268.  
  269.     enum MEDIA_TYPES   {MT_Y41P = 0, MT_YUY2, MT_UYVY, MT_RGB24, MT_RGB565,
  270.                         MT_RGB555, MT_RGB8, MT_COUNT_OF_TYPES = MT_RGB8};
  271.  
  272.     enum PALETTE_TYPE {COLOUR_PALETTE, GREY_PALETTE};
  273.  
  274.     PALETTE_TYPE    m_PaletteType;
  275.     BOOL            m_IgnoreQualityMessage;
  276.     DWORD           m_dwOutputFormatDib;
  277.     DWORD           m_dwOutputFormatDdb;
  278.  
  279.     HRESULT         CopySampleToBuffer(IMediaSample *pSample);
  280.     BOOL            UpdateTimeSyncPoint(LPBYTE lpPicStart, REFERENCE_TIME *Time);
  281.     ptrdiff_t       BuffOffset(LPBYTE Offset);
  282.  
  283.     HRESULT         DecodeNextPicture();
  284.     void            ResetVideoDecoder();
  285.     void            DecodeUntilBufferEmpty();
  286.  
  287.     void            InitDestinationVideoInfo(VIDEOINFO *pVI, DWORD Comp, int n);
  288.     void            InitDestinationPalette(VIDEOINFO *pVideoInfo);
  289.  
  290.     BOOL            ParseSequenceHeader(const BYTE *pbData, LONG lData,
  291.                                         SEQHDR_INFO *pInfo);
  292.  
  293.     //
  294.     //  Stop time
  295.     //
  296.     CRefTime        m_tStop;
  297. };
  298. #endif
  299.