2 -----------------------------------------------------------------------------
3 This source file is part of VideoTexture library
5 Copyright (c) 2007 The Zdeno Ash Miklas
7 This program is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
19 http://www.gnu.org/copyleft/lesser.txt.
20 -----------------------------------------------------------------------------
23 /** \file VideoFFmpeg.h
24 * \ingroup bgevideotex
27 #ifndef __VIDEOFFMPEG_H__
28 #define __VIDEOFFMPEG_H__
31 /* this needs to be parsed with __cplusplus defined before included through ffmpeg_compat.h */
32 #if defined(__FreeBSD__)
33 # include <inttypes.h>
37 #include "ffmpeg_compat.h"
38 #include "DNA_listBase.h"
39 #include "BLI_threads.h"
40 #include "BLI_blenlib.h"
43 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
44 # define FFMPEG_OLD_FRAME_RATE 1
46 # define FFMPEG_CODEC_IS_POINTER 1
49 #if LIBAVFORMAT_VERSION_INT >= (52 << 16)
50 # define FFMPEG_PB_IS_POINTER 1
53 #ifdef FFMPEG_CODEC_IS_POINTER
54 static inline AVCodecContext* get_codec_from_stream(AVStream* stream)
59 static inline AVCodecContext* get_codec_from_stream(AVStream* stream)
61 return &stream->codec;
65 #include "VideoBase.h"
67 #define CACHE_FRAME_SIZE 10
68 #define CACHE_PACKET_SIZE 30
70 // type VideoFFmpeg declaration
71 class VideoFFmpeg : public VideoBase
75 VideoFFmpeg (HRESULT * hRslt);
77 virtual ~VideoFFmpeg ();
79 /// set initial parameters
80 void initParams (short width, short height, float rate, bool image=false);
81 /// open video/image file
82 virtual void openFile (char * file);
83 /// open video capture device
84 virtual void openCam (char * driver, short camIdx);
86 /// release video source
87 virtual bool release (void);
90 virtual bool play (void);
92 virtual bool pause (void);
94 virtual bool stop (void);
96 virtual void setRange (double start, double stop);
98 virtual void setFrameRate (float rate);
99 // some specific getters and setters
100 int getPreseek(void) { return m_preseek; }
101 void setPreseek(int preseek) { if (preseek >= 0) m_preseek = preseek; }
102 bool getDeinterlace(void) { return m_deinterlace; }
103 void setDeinterlace(bool deinterlace) { m_deinterlace = deinterlace; }
104 char *getImageName(void) { return (m_isImage) ? m_imageName.Ptr() : NULL; }
107 // format and codec information
109 AVFormatContext *m_formatCtx;
110 AVCodecContext *m_codecCtx;
111 // raw frame extracted from video file
113 // deinterlaced frame if codec requires it
114 AVFrame *m_frameDeinterlaced;
115 // decoded RGB24 frame if codec requires it
117 // conversion from raw to RGB is done with sws_scale
118 struct SwsContext *m_imgConvertCtx;
119 // should the codec be deinterlaced?
121 // number of frame of preseek
123 // order number of stream holding the video in format context
126 // the actual frame rate
127 double m_baseFrameRate;
129 /// last displayed frame
132 /// end of file reached
135 /// flag to indicate that time is coming from application
138 /// current file pointer position in file expressed in frame number
141 /// time of video play start
144 /// width of capture in pixel
147 /// height of capture in pixel
150 /// frame rate of capture in frames per seconds
153 /// is file an image?
156 /// is image loading done in a separate thread?
159 /// is streaming or camera?
162 /// keep last image name
163 STR_String m_imageName;
165 /// image calculation
166 virtual void calcImage (unsigned int texId, double ts);
168 /// set actual position
169 void setPositions (void);
171 /// get actual framerate
172 double actFrameRate (void) { return m_frameRate * m_baseFrameRate; }
174 /// common function to video file and capture
175 int openStream(const char *filename, AVInputFormat *inputFormat, AVFormatParameters *formatParams);
177 /// check if a frame is available and load it in pFrame, return true if a frame could be retrieved
178 AVFrame* grabFrame(long frame);
180 /// in case of caching, put the frame back in free queue
181 void releaseFrame(AVFrame* frame);
183 /// start thread to load the video file/capture/stream
201 ListBase m_frameCacheBase; // list of frames that are ready
202 ListBase m_frameCacheFree; // list of frames that are unused
203 ListBase m_packetCacheBase; // list of packets that are ready for decoding
204 ListBase m_packetCacheFree; // list of packets that are unused
205 pthread_mutex_t m_cacheMutex;
207 AVFrame *allocFrameRGB();
208 static void *cacheThread(void *);
211 inline VideoFFmpeg * getFFmpeg (PyImage * self)
213 return static_cast<VideoFFmpeg*>(self->m_image);