Blender V2.61 - r43446

VideoFFmpeg.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of VideoTexture library
00004 
00005 Copyright (c) 2007 The Zdeno Ash Miklas
00006 
00007 This program is free software; you can redistribute it and/or modify it under
00008 the terms of the GNU Lesser General Public License as published by the Free Software
00009 Foundation; either version 2 of the License, or (at your option) any later
00010 version.
00011 
00012 This program is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License along with
00017 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00018 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00019 http://www.gnu.org/copyleft/lesser.txt.
00020 -----------------------------------------------------------------------------
00021 */
00022 
00027 #if !defined VIDEOFFMPEG_H
00028 #define VIDEOFFMPEG_H
00029 
00030 #ifdef WITH_FFMPEG
00031 extern "C" {
00032 #undef __cplusplus
00033 #include <pthread.h>
00034 
00035 #include "ffmpeg_compat.h"
00036 
00037 #include "DNA_listBase.h"
00038 #include "BLI_threads.h"
00039 #include "BLI_blenlib.h"
00040 #define __cplusplus
00041 }
00042 
00043 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
00044 #define FFMPEG_OLD_FRAME_RATE 1
00045 #else
00046 #define FFMPEG_CODEC_IS_POINTER 1
00047 #endif
00048 
00049 #if LIBAVFORMAT_VERSION_INT >= (52 << 16)
00050 #define FFMPEG_PB_IS_POINTER 1
00051 #endif
00052 
00053 #ifdef FFMPEG_CODEC_IS_POINTER
00054 static inline AVCodecContext* get_codec_from_stream(AVStream* stream)
00055 {
00056     return stream->codec;
00057 }
00058 #else
00059 static inline AVCodecContext* get_codec_from_stream(AVStream* stream)
00060 {
00061     return &stream->codec;
00062 }
00063 #endif
00064 
00065 #include "VideoBase.h"
00066 
00067 #define CACHE_FRAME_SIZE    10
00068 #define CACHE_PACKET_SIZE   30
00069 
00070 // type VideoFFmpeg declaration
00071 class VideoFFmpeg : public VideoBase
00072 {
00073 public:
00075     VideoFFmpeg (HRESULT * hRslt);
00077     virtual ~VideoFFmpeg ();
00078 
00080     void initParams (short width, short height, float rate, bool image=false);
00082     virtual void openFile (char * file);
00084     virtual void openCam (char * driver, short camIdx);
00085 
00087     virtual bool release (void);
00088 
00090     virtual bool play (void);
00092     virtual bool pause (void);
00094     virtual bool stop (void);
00096     virtual void setRange (double start, double stop);
00098     virtual void setFrameRate (float rate);
00099     // some specific getters and setters
00100     int getPreseek(void) { return m_preseek; }
00101     void setPreseek(int preseek) { if (preseek >= 0) m_preseek = preseek; }
00102     bool getDeinterlace(void) { return m_deinterlace; }
00103     void setDeinterlace(bool deinterlace) { m_deinterlace = deinterlace; }
00104     char *getImageName(void) { return (m_isImage) ? m_imageName.Ptr() : NULL; }
00105 
00106 protected:
00107     // format and codec information
00108     AVCodec *m_codec;
00109     AVFormatContext *m_formatCtx;
00110     AVCodecContext *m_codecCtx;
00111     // raw frame extracted from video file
00112     AVFrame *m_frame;
00113     // deinterlaced frame if codec requires it
00114     AVFrame *m_frameDeinterlaced;
00115     // decoded RGB24 frame if codec requires it
00116     AVFrame *m_frameRGB;
00117     // conversion from raw to RGB is done with sws_scale
00118     struct SwsContext *m_imgConvertCtx;
00119     // should the codec be deinterlaced?
00120     bool m_deinterlace;
00121     // number of frame of preseek
00122     int m_preseek;
00123     // order number of stream holding the video in format context
00124     int m_videoStream;
00125 
00126     // the actual frame rate
00127     double m_baseFrameRate;
00128 
00130     long m_lastFrame;
00131 
00133     bool m_eof;
00134 
00136     bool m_externTime;
00137 
00139     long m_curPosition;
00140 
00142     double m_startTime;
00143 
00145     short m_captWidth;
00146     
00148     short m_captHeight;
00149 
00151     float m_captRate;
00152 
00154     bool m_isImage;
00155 
00157     bool m_isThreaded;
00158 
00160     bool m_isStreaming;
00161 
00163     STR_String m_imageName;
00164 
00166     virtual void calcImage (unsigned int texId, double ts);
00167 
00169     void setPositions (void);
00170 
00172     double actFrameRate (void) { return m_frameRate * m_baseFrameRate; }
00173 
00175     int openStream(const char *filename, AVInputFormat *inputFormat, AVFormatParameters *formatParams);
00176 
00178     AVFrame* grabFrame(long frame);
00179 
00181     void releaseFrame(AVFrame* frame);
00182 
00184     bool startCache();
00185     void stopCache();
00186 
00187 private:
00188     typedef struct {
00189         Link link;
00190         long framePosition;
00191         AVFrame *frame;
00192     } CacheFrame;
00193     typedef struct {
00194         Link link;
00195         AVPacket packet;
00196     } CachePacket;
00197 
00198     bool m_stopThread;
00199     bool m_cacheStarted;
00200     ListBase m_thread;
00201     ListBase m_frameCacheBase;  // list of frames that are ready
00202     ListBase m_frameCacheFree;  // list of frames that are unused
00203     ListBase m_packetCacheBase; // list of packets that are ready for decoding
00204     ListBase m_packetCacheFree; // list of packets that are unused
00205     pthread_mutex_t m_cacheMutex;
00206 
00207     AVFrame *allocFrameRGB();
00208     static void *cacheThread(void *);
00209 };
00210 
00211 inline VideoFFmpeg * getFFmpeg (PyImage * self) 
00212 {
00213     return static_cast<VideoFFmpeg*>(self->m_image); 
00214 }
00215 
00216 #endif  //WITH_FFMPEG
00217 
00218 #endif