EMIPLIB

mipavcodecdecoder.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 MIPAVCODECDECODER_H
00030 
00031 #define MIPAVCODECDECODER_H
00032 
00033 #include "mipconfig.h"
00034 
00035 #ifdef MIPCONFIG_SUPPORT_AVCODEC
00036 
00037 #include "mipcomponent.h"
00038 #include "miptime.h"
00039 
00040 #ifdef MIPCONFIG_SUPPORT_AVCODEC_OLD
00041         #include <ffmpeg/avcodec.h>
00042 #else
00043         extern "C" 
00044         {       
00045                 #include <libavcodec/avcodec.h>
00046                 #include <libswscale/swscale.h>
00047                 #include <libavformat/avformat.h>
00048         }
00049 #endif // MIPCONFIG_SUPPORT_AVCODEC_OLD
00050 
00051 #if defined(WIN32) || defined(_WIN32_WCE)
00052         #include <hash_map>
00053 #else
00054         #include <ext/hash_map>
00055 #endif // Win32
00056 #include <list>
00057 
00058 class MIPRawYUV420PVideoMessage;
00059 
00064 class EMIPLIB_IMPORTEXPORT MIPAVCodecDecoder : public MIPComponent
00065 {
00066 public:
00067         MIPAVCodecDecoder();
00068         ~MIPAVCodecDecoder();
00069 
00076         bool init(bool waitForKeyframe);
00077 
00079         bool destroy();
00080         bool push(const MIPComponentChain &chain, int64_t iteration, MIPMessage *pMsg);
00081         bool pull(const MIPComponentChain &chain, int64_t iteration, MIPMessage **pMsg);
00082 
00087         static void initAVCodec();
00088 private:
00089         void clearMessages();
00090         void expire();
00091 
00092         class DecoderInfo
00093         {
00094         public:
00095 #ifndef MIPCONFIG_SUPPORT_AVCODEC_OLD
00096                 DecoderInfo(int w, int h, AVCodecContext *pContext, SwsContext *pSwsContext)
00097 #else
00098                 DecoderInfo(int w, int h, AVCodecContext *pContext)
00099 #endif // MIPCONFIG_SUPPORT_AVCODEC_OLD
00100                 {
00101                         m_width = w;
00102                         m_height = h;
00103                         m_pContext = pContext;
00104                         m_lastTime = MIPTime::getCurrentTime();
00105 #ifndef MIPCONFIG_SUPPORT_AVCODEC_OLD
00106                         m_pSwsContext = pSwsContext;
00107 #endif // MIPCONFIG_SUPPORT_AVCODEC_OLD
00108                         m_gotKeyframe = false;
00109                 }
00110 
00111                 ~DecoderInfo()
00112                 {
00113                         avcodec_close(m_pContext);
00114                         av_free(m_pContext);
00115 #ifndef MIPCONFIG_SUPPORT_AVCODEC_OLD
00116                         if (m_pSwsContext)
00117                                 sws_freeContext(m_pSwsContext);
00118 #endif // MIPCONFIG_SUPPORT_AVCODEC_OLD
00119                 }
00120 
00121                 int getWidth() const                                                    { return m_width; }
00122                 int getHeight() const                                                   { return m_height; }
00123                 AVCodecContext *getContext()                                            { return m_pContext; }
00124 #ifndef MIPCONFIG_SUPPORT_AVCODEC_OLD
00125                 SwsContext *getSwsContext()                                             { return m_pSwsContext; }
00126                 void setSwsContext(SwsContext *pCtx)                                    { m_pSwsContext = pCtx; }
00127 #endif // MIPCONFIG_SUPPORT_AVCODEC_OLD
00128                 bool receivedKeyframe() const                                           { return m_gotKeyframe; }
00129                 void setReceivedKeyframe(bool f)                                        { m_gotKeyframe = f; }
00130 
00131                 MIPTime getLastUpdateTime() const                                       { return m_lastTime; }
00132                 void setLastUpdateTime(MIPTime t)                                       { m_lastTime = t; }
00133         private:
00134                 int m_width, m_height;
00135                 AVCodecContext *m_pContext;
00136                 MIPTime m_lastTime;
00137 #ifndef MIPCONFIG_SUPPORT_AVCODEC_OLD
00138                 SwsContext *m_pSwsContext;
00139 #endif // MIPCONFIG_SUPPORT_AVCODEC_OLD
00140                 bool m_gotKeyframe;
00141         };
00142         
00143         bool m_init;
00144         
00145 #if defined(WIN32) || defined(_WIN32_WCE)
00146         stdext::hash_map<uint64_t, DecoderInfo *> m_decoderStates;
00147 #else
00148         __gnu_cxx::hash_map<uint64_t, DecoderInfo *, __gnu_cxx::hash<uint32_t> > m_decoderStates;
00149 #endif // Win32
00150         
00151         AVCodec *m_pCodec;
00152         AVFrame *m_pFrame;
00153 #ifdef MIPCONFIG_SUPPORT_AVCODEC_OLD
00154         AVFrame *m_pFrameYUV420P;
00155 #endif // MIPCONFIG_SUPPORT_AVCODEC_OLD
00156 
00157         std::list<MIPRawYUV420PVideoMessage *> m_messages;
00158         std::list<MIPRawYUV420PVideoMessage *>::const_iterator m_msgIt;
00159         int64_t m_lastIteration;
00160         MIPTime m_lastExpireTime;
00161         bool m_waitForKeyframe;
00162 };
00163 
00164 #endif // MIPCONFIG_SUPPORT_AVCODEC
00165 
00166 #endif // MIPAVCODECDECODER_H
00167