EMIPLIB

mipavcodecframeconverter.h

Go to the documentation of this file.
00001 /*
00002     
00003   This file is a part of EMIPLIB, the EDM Media over IP Library.
00004   
00005   Copyright (C) 2006-2011  Hasselt University - Expertise Centre for
00006                       Digital Media (EDM) (http://www.edm.uhasselt.be)
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00021   USA
00022 
00023 */
00024 
00029 #ifndef MIPAVCODECFRAMECONVERTER_H
00030 
00031 #define MIPAVCODECFRAMECONVERTER_H
00032 
00033 #include "mipconfig.h"
00034 
00035 #ifdef MIPCONFIG_SUPPORT_AVCODEC
00036 
00037 #include "mipcomponent.h"
00038 #include "miptime.h"
00039 #if defined(WIN32) || defined(_WIN32_WCE)
00040         #include <hash_map>
00041 #else
00042         #include <ext/hash_map>
00043 #endif // Win32
00044 
00045 #ifdef MIPCONFIG_SUPPORT_AVCODEC_OLD
00046         #include <ffmpeg/avcodec.h>     
00047         #include <ffmpeg/avutil.h>      
00048 #else
00049         extern "C" 
00050         {       
00051                 #include <libavcodec/avcodec.h>
00052                 #include <libswscale/swscale.h>
00053         }
00054 #endif // MIPCONFIG_SUPPORT_AVCODEC_OLD
00055 
00056 #include <list>
00057 
00058 class MIPVideoMessage;
00059 
00064 class EMIPLIB_IMPORTEXPORT MIPAVCodecFrameConverter : public MIPComponent
00065 {
00066 public:
00067         MIPAVCodecFrameConverter();
00068         ~MIPAVCodecFrameConverter();
00069 
00076         bool init(int targetWidth, int targetHeight, uint32_t targetSubtype);
00077 
00079         bool destroy();
00080 
00081         bool push(const MIPComponentChain &chain, int64_t iteration, MIPMessage *pMsg);
00082         bool pull(const MIPComponentChain &chain, int64_t iteration, MIPMessage **pMsg);
00083 
00088         static void initAVCodec();
00089 private:
00090         void clearMessages();
00091         void expire();
00092         void clearCache();
00093 
00094         bool m_init;
00095         std::list<MIPVideoMessage *> m_messages;
00096         std::list<MIPVideoMessage *>::const_iterator m_msgIt;
00097         int64_t m_lastIteration;
00098 
00099         int m_targetWidth;
00100         int m_targetHeight;
00101         uint32_t m_targetSubtype;
00102         PixelFormat m_targetPixFmt;
00103 
00104 #ifdef MIPCONFIG_SUPPORT_AVCODEC_OLD
00105         void *m_pTargetAVFrame;
00106         void *m_pSrcAVFrame;
00107 #else 
00108         class ConvertCache 
00109         {
00110         public:
00111                 ConvertCache(int srcWidth, int srcHeight, uint32_t srcSubtype, SwsContext *pSwsContext)
00112                 {
00113                         m_srcWidth = srcWidth;
00114                         m_srcHeight = srcHeight;
00115                         m_srcSubtype = srcSubtype;
00116                         m_pSwsContext = pSwsContext;
00117                         m_lastTime = MIPTime::getCurrentTime();
00118                 }
00119 
00120                 ~ConvertCache()
00121                 {
00122                         sws_freeContext(m_pSwsContext);
00123                 }
00124 
00125                 int getSourceWidth() const                                              { return m_srcWidth; }
00126                 int getSourceHeight() const                                             { return m_srcHeight; }
00127                 uint32_t getSourceSubtype() const                                       { return m_srcSubtype; }
00128                 SwsContext *getSwsContext()                                             { return m_pSwsContext; }
00129 
00130                 MIPTime getLastUpdateTime() const                                       { return m_lastTime; }
00131                 void setLastUpdateTime(MIPTime t)                                       { m_lastTime = t; }
00132         private:
00133                 SwsContext *m_pSwsContext;
00134                 int m_srcWidth;
00135                 int m_srcHeight;
00136                 uint32_t m_srcSubtype;
00137                 MIPTime m_lastTime;
00138         };
00139 #if defined(WIN32) || defined(_WIN32_WCE)
00140         stdext::hash_map<uint64_t, ConvertCache *> m_convertCache;
00141 #else
00142         __gnu_cxx::hash_map<uint64_t, ConvertCache *, __gnu_cxx::hash<uint32_t> > m_convertCache;
00143 #endif // Win32
00144         MIPTime m_lastExpireTime;
00145 #endif // MIPCONFIG_SUPPORT_AVCODEC_OLD
00146 };
00147 
00148 #endif // MIPCONFIG_SUPPORT_AVCODEC
00149 
00150 #endif // MIPAVCODECFRAMECONVERTER_H
00151