home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / decoder.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-29  |  3.3 KB  |  132 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: Decoder.cpp
  14. *
  15. * Implements a prototype Mpeg Audio Software codec.  It just consumes
  16. * the passed in audio frames.
  17. *
  18. *
  19. \**************************************************************************/
  20. #include "MpgAudio.h"
  21.  
  22.  
  23.  
  24. /* -------------------------------------------------------------------------
  25. ** CAudioDecoder
  26. ** -------------------------------------------------------------------------
  27. */
  28.  
  29. /******************************Public*Routine******************************\
  30. * CAudioDecoder
  31. *
  32. * Constructor and destructor
  33. *
  34. *
  35. \**************************************************************************/
  36. CAudioDecoder::CAudioDecoder(CMpegAudioCodec *pMpegCodec)
  37.     : m_pCodec(pMpegCodec) {}
  38.  
  39.  
  40.  
  41. /******************************Public*Routine******************************\
  42. * ResetAudio
  43. *
  44. *
  45. \**************************************************************************/
  46. DWORD
  47. CAudioDecoder::ResetAudio()
  48. {
  49.     CAutoLock lck(this);
  50.     return DECODE_SUCCESS;
  51. }
  52.  
  53.  
  54. /******************************Public*Routine******************************\
  55. * DecodeFrame
  56. *
  57. *
  58. \**************************************************************************/
  59. DWORD
  60. CAudioDecoder::DecodeAudioFrame(
  61.     AudioCtrl *pCtrl
  62.     )
  63. {
  64.     CAutoLock   lck(this);
  65.     DWORD       rc = DECODE_SUCCESS;
  66.  
  67.     m_pCtrl = pCtrl;
  68.  
  69.  
  70.     for (DWORD i = 0; i < m_pCtrl->dwNumFrames; i++) {
  71.  
  72.         // Skip the sync word
  73.         m_pCtrl->pCmprRead += 2;
  74.  
  75.         // skip the rest of the frame
  76.         if (!SkipFrame()) {
  77.             m_pCtrl->dwOutBuffUsed = 0L;
  78.             return DECODE_ERR_DATA;
  79.         }
  80.     }
  81.  
  82.     ZeroMemory(m_pCtrl->pOutBuffer, m_pCtrl->dwOutBuffSize);
  83.     m_pCtrl->dwOutBuffUsed = m_pCtrl->dwOutBuffSize;
  84.  
  85.     return rc;
  86. }
  87.  
  88.  
  89. /*****************************Private*Routine******************************\
  90. * SkipFrame
  91. *
  92. *
  93. \**************************************************************************/
  94. BOOL
  95. CAudioDecoder::SkipFrame()
  96. {
  97.     LPBYTE  lpCurr = m_pCtrl->pCmprRead;
  98.     LPBYTE  lpEnd = m_pCtrl->pCmprWrite;
  99.     int sm = 0;
  100.  
  101.     while (lpCurr < lpEnd && sm < 2)  {
  102.  
  103.         switch (sm) {
  104.         case 0:
  105.             sm = (*lpCurr == 0xff);
  106.             break;
  107.  
  108.         case 1:
  109.             if ((*lpCurr & 0xf0) == 0xf0) sm = 2; /* sync found */
  110.             else sm = (*lpCurr == 0xff);
  111.             break;
  112.         }
  113.         lpCurr++;
  114.     }
  115.  
  116.     //
  117.     // When we get here we have either run out of buffer or found the first
  118.     // "sm" bytes of a valid sync word.
  119.     //
  120.     // Don't forget to put back the sync word bytes that we have just
  121.     // read otherwise they would be lost forever.
  122.     //
  123.     lpCurr -= sm;
  124.     m_pCtrl->pCmprRead = lpCurr;
  125.  
  126.     if (sm < 2) {
  127.         return FALSE;   // sync not found.
  128.     }
  129.  
  130.     return TRUE;
  131. }
  132.