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

  1. /* 
  2.  *  FrameBuffer.h
  3.  *
  4.  *    Copyright (C) Alberto Vigata - July 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.  
  25. #ifndef FRAMEBUFFER_H
  26. #define FRAMEBUFFER_H
  27.  
  28.  
  29.  
  30.  
  31. #include <windows.h>
  32. #include "flasktypes.h"
  33. #include "framesource.h"
  34. #include "thread.h"
  35. #include <list>
  36. using namespace std;
  37.  
  38.  
  39. class CListFrameBuffer : public CFrameBuffer
  40. {
  41. public:
  42.     CListFrameBuffer(int nWidth, int nHeight, int nFormat, int nElements)
  43.     {
  44.       CFrame *pFrame;
  45.       for(int i=0; i<nElements; i++)
  46.       {
  47.         pFrame = new CFrame(this);
  48.         pFrame->Set( nWidth, nHeight, nFormat);
  49.  
  50.         // Add frame to the list
  51.         m_lsFrames.push_back(pFrame);
  52.       }
  53.       m_evListIsNotEmpty.Reset();
  54.     }
  55.     ~CListFrameBuffer()
  56.     {
  57.       // Delete frames in the list
  58.       int nSize = m_lsFrames.size();
  59.       CFrame *pFrame;
  60.       for(int i=0; i<nSize; i++)
  61.       {
  62.         pFrame = m_lsFrames.front();
  63.         delete pFrame;
  64.         m_lsFrames.pop_front();
  65.       }
  66.     }
  67.  
  68.     CFrame *GetFreeFrame()
  69.     {
  70.       int nSize;
  71.       {
  72.         CFlAutoLock lockObject(&m_csObject);
  73.         nSize = m_lsFrames.size();
  74.       }
  75.       // If there were no frames wait for one
  76.       if(!nSize)
  77.         m_evListIsNotEmpty.Wait();
  78.       {
  79.         CFlAutoLock lockObject(&m_csObject);
  80.         // Now grab one      
  81.         CFrame *pFrame = m_lsFrames.front();
  82.         // Add ref frame
  83.         pFrame->AddRef();
  84.         // remove frame from queue
  85.         m_lsFrames.pop_front();
  86.         //
  87.         if(!m_lsFrames.size())
  88.           m_evListIsNotEmpty.Reset();
  89.         return pFrame;
  90.       }
  91.  
  92.     }
  93.     int GetSize()
  94.     {
  95.       CFlAutoLock lockObject(&m_csObject);
  96.       return m_lsFrames.size();
  97.     }    
  98.     // Add this frame to the free list
  99.     void AddFreeFrame(CFrame *pFrame)
  100.     {
  101.       CFlAutoLock lockObject(&m_csObject);
  102.       m_lsFrames.push_back(pFrame);
  103.       // And signal that the queue is not empty
  104.       m_evListIsNotEmpty.Set();
  105.     }
  106.  
  107. private:
  108.   CFlCritSec m_csObject;
  109.   CFlEvent m_evListIsNotEmpty;
  110.   list<CFrame *> m_lsFrames;
  111. };
  112.  
  113.  
  114. // This queue just stores the pointers. It doesnt own the buffers.
  115. class CQueueFrameBuffer
  116. {
  117. public:
  118.   CQueueFrameBuffer()
  119.   {
  120.     m_lsFrames.clear();
  121.     m_evListIsNotEmpty.Reset();
  122.   }
  123.   ~CQueueFrameBuffer()
  124.   {
  125.     // Delete frames in the list
  126.     int nSize = m_lsFrames.size();
  127.     CFrame *pFrame;
  128.     for(int i=0; i<nSize; i++)
  129.     {
  130.       pFrame = m_lsFrames.front();
  131.       delete pFrame;
  132.       m_lsFrames.pop_front();
  133.     }
  134.   }
  135.  
  136.   int GetSize()
  137.   {
  138.     CFlAutoLock lockObject(&m_csObject);
  139.     return m_lsFrames.size();
  140.   }
  141.   
  142.   CFrame *RemoveFrame()
  143.   {
  144.     int nSize;
  145.     {
  146.       CFlAutoLock lockObject(&m_csObject);
  147.       nSize = m_lsFrames.size();
  148.     }
  149.     // If there were no frames wait for one
  150.     if(!nSize)
  151.       m_evListIsNotEmpty.Wait();
  152.     {
  153.       CFlAutoLock lockObject(&m_csObject);    
  154.       // Now grab one      
  155.       CFrame *pFrame = m_lsFrames.front();
  156.  
  157.       // remove frame from queue
  158.       m_lsFrames.pop_front();
  159.       if(!m_lsFrames.size())
  160.         m_evListIsNotEmpty.Reset();
  161.     return pFrame;      
  162.     }
  163.  
  164.   }
  165.   
  166.   // Add this frame to the free list
  167.   void AddFrame(CFrame *pFrame)
  168.   {
  169.     CFlAutoLock lockObject(&m_csObject);
  170.     m_lsFrames.push_back(pFrame);
  171.     // And signal that the queue is not empty
  172.     m_evListIsNotEmpty.Set();
  173.   }
  174.   
  175. private:
  176.   CFlCritSec m_csObject;
  177.   CFlEvent m_evListIsNotEmpty;
  178.   list<CFrame *> m_lsFrames;
  179. };
  180.  
  181.  
  182. #endif