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

  1. // VideoSource.cpp: implementation of the CVideoSource class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "VideoSource.h"
  6. #include "postprocessing.h"
  7. #include "debug.h"
  8.  
  9.  
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12.  
  13. CVideoSource::CVideoSource()
  14. {
  15.   m_nWidth = m_nHeight = 0;
  16. }
  17.  
  18. //  
  19. // Actual running thread
  20.  
  21. ui32 CVideoSource::GetFrame(CFrame **ppFrame)
  22. {
  23.   // If there are more than 8 frames to be
  24.   // processed in the queue
  25.   // awake the decoder to start working
  26.   if(m_pProcessedQueue->GetSize()>=8)
  27.     ScheduleCommand(getFrame);
  28.  
  29.   DBG_STR((str, "%d ", m_pProcessedQueue->GetSize() ));
  30.  
  31.   if(!(*ppFrame = m_pOutputQueue->RemoveFrame()))
  32.     return FRAME_NOTPRESENT;
  33.   if((*ppFrame)->IsLastFrame())
  34.     return FRAME_NOTPRESENT;
  35.  
  36.   return FRAME_OK;
  37. }
  38.  
  39. bool CVideoSource::Start( CFrameSource *pSource, TPPost *pp )
  40. {
  41.   m_pFrameSource = pSource; 
  42.  
  43.   if( !pp )
  44.     return false;
  45.  
  46.   if( !m_pp.Set(pp) )
  47.     return false;
  48.   if( !m_pp.Start() )
  49.     return false;
  50.  
  51.   m_nWidth = m_pp.GetWidth();
  52.   m_nHeight = m_pp.GetHeight();
  53.  
  54.   m_pProcessedQueue = new CListFrameBuffer( m_nWidth, m_nHeight, pp->nProcessingFormat, 10);
  55.   
  56.   m_pOutputQueue    = new CQueueFrameBuffer();
  57.   
  58.   ScheduleCommand(noCommand);
  59.   
  60.   Create();
  61.   return true;
  62. }
  63.  
  64. bool CVideoSource::Stop()
  65. {
  66.   // Terminate the thread
  67.   Exit();
  68.   
  69.   m_pp.Stop();
  70.  
  71.   if( m_pProcessedQueue )
  72.   {
  73.     delete m_pProcessedQueue;
  74.     m_pProcessedQueue = NULL;
  75.   }
  76.     
  77.   
  78.   if( m_pOutputQueue )
  79.   {
  80.     delete m_pOutputQueue;
  81.     m_pOutputQueue = NULL;
  82.   }
  83.  
  84.   return true;
  85. }
  86.  
  87. DWORD CVideoSource::ThreadProc()
  88. {
  89.   m_nState = stStopped;
  90.  
  91.   TCommand sCommand;
  92.   CFrame *pOutputFrame;
  93.   CFrame *pDecodedFrame;
  94.  
  95.   while(1)
  96.   {
  97.     if(GetCommand(&sCommand))
  98.     {
  99.       switch( sCommand.nCommand )
  100.       {
  101.       case noCommand:
  102.         break;
  103.       case getFrame:
  104.         m_nState = stGetFrame;
  105.         break;
  106.       case exit:
  107.         // Exit
  108.         ReplyCommand(&sCommand);
  109.         return 0;
  110.         break;
  111.       }
  112.       ReplyCommand(&sCommand);
  113.     }
  114.     else
  115.     {
  116.       // If we are stopped, wait for a command
  117.       if(m_nState==stStopped)
  118.         WaitCommand();
  119.     }
  120.     switch( m_nState )
  121.     {
  122.     case stGetFrame:
  123.       
  124.       // If the queue is almost empty means that you've processed
  125.       // all those.
  126.       // stop
  127.       if(m_pProcessedQueue->GetSize()<=2)
  128.       {
  129.         m_nState = stStopped;
  130.         break;
  131.       }   
  132.  
  133.       // Get a free frame from the processed buffer
  134.       if(!(pOutputFrame = m_pProcessedQueue->GetFreeFrame() ))
  135.       {
  136.         m_nState = stStopped;
  137.         break;
  138.       }
  139.  
  140.       // Get a new frame
  141.       if(m_pFrameSource->GetFrame(&pDecodedFrame))
  142.       {
  143.         m_pp.Process(pDecodedFrame, pOutputFrame);
  144.         // Add time info
  145.         pOutputFrame->SetPresTime( pDecodedFrame->GetPresTime() );
  146.         // Add processed frame to the output queue
  147.         m_pOutputQueue->AddFrame( pOutputFrame );
  148.         // Release decoded
  149.         pDecodedFrame->Release();
  150.       }
  151.       else
  152.       {
  153.         // Likely the last frame in the stream
  154.         pOutputFrame->Erase();
  155.         pOutputFrame->SetLastFrame();
  156.  
  157.         m_pOutputQueue->AddFrame( pOutputFrame );
  158.  
  159.       }
  160.       break; 
  161.     }
  162.   }
  163. }