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 #if !defined VIDEOFFMPEG_H
34 #include <libavformat/avformat.h>
35 #include <libavcodec/avcodec.h>
36 #include <libavutil/rational.h>
37 #if (LIBAVFORMAT_VERSION_MAJOR > 52) || ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 101))
38 #include <libavutil/parseutils.h>
40 #include <libswscale/swscale.h>
41 #include "DNA_listBase.h"
42 #include "BLI_threads.h"
43 #include "BLI_blenlib.h"
48 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
49 #define FFMPEG_OLD_FRAME_RATE 1
51 #define FFMPEG_CODEC_IS_POINTER 1
54 #if LIBAVFORMAT_VERSION_INT >= (52 << 16)
55 #define FFMPEG_PB_IS_POINTER 1
58 #ifdef FFMPEG_CODEC_IS_POINTER
59 static inline AVCodecContext* get_codec_from_stream(AVStream* stream)
64 static inline AVCodecContext* get_codec_from_stream(AVStream* stream)
66 return &stream->codec;
70 #include "VideoBase.h"
72 #define CACHE_FRAME_SIZE 10
73 #define CACHE_PACKET_SIZE 30
75 // type VideoFFmpeg declaration
76 class VideoFFmpeg : public VideoBase
80 VideoFFmpeg (HRESULT * hRslt);
82 virtual ~VideoFFmpeg ();
84 /// set initial parameters
85 void initParams (short width, short height, float rate, bool image=false);
86 /// open video/image file
87 virtual void openFile (char * file);
88 /// open video capture device
89 virtual void openCam (char * driver, short camIdx);
91 /// release video source
92 virtual bool release (void);
95 virtual bool play (void);
97 virtual bool pause (void);
99 virtual bool stop (void);
101 virtual void setRange (double start, double stop);
103 virtual void setFrameRate (float rate);
104 // some specific getters and setters
105 int getPreseek(void) { return m_preseek; }
106 void setPreseek(int preseek) { if (preseek >= 0) m_preseek = preseek; }
107 bool getDeinterlace(void) { return m_deinterlace; }
108 void setDeinterlace(bool deinterlace) { m_deinterlace = deinterlace; }
109 char *getImageName(void) { return (m_isImage) ? m_imageName.Ptr() : NULL; }
112 // format and codec information
114 AVFormatContext *m_formatCtx;
115 AVCodecContext *m_codecCtx;
116 // raw frame extracted from video file
118 // deinterlaced frame if codec requires it
119 AVFrame *m_frameDeinterlaced;
120 // decoded RGB24 frame if codec requires it
122 // conversion from raw to RGB is done with sws_scale
123 struct SwsContext *m_imgConvertCtx;
124 // should the codec be deinterlaced?
126 // number of frame of preseek
128 // order number of stream holding the video in format context
131 // the actual frame rate
132 double m_baseFrameRate;
134 /// last displayed frame
137 /// end of file reached
140 /// flag to indicate that time is coming from application
143 /// current file pointer position in file expressed in frame number
146 /// time of video play start
149 /// width of capture in pixel
152 /// height of capture in pixel
155 /// frame rate of capture in frames per seconds
158 /// is file an image?
161 /// is image loading done in a separate thread?
164 /// is streaming or camera?
167 /// keep last image name
168 STR_String m_imageName;
170 /// image calculation
171 virtual void calcImage (unsigned int texId, double ts);
173 /// set actual position
174 void setPositions (void);
176 /// get actual framerate
177 double actFrameRate (void) { return m_frameRate * m_baseFrameRate; }
179 /// common function to video file and capture
180 int openStream(const char *filename, AVInputFormat *inputFormat, AVFormatParameters *formatParams);
182 /// check if a frame is available and load it in pFrame, return true if a frame could be retrieved
183 AVFrame* grabFrame(long frame);
185 /// in case of caching, put the frame back in free queue
186 void releaseFrame(AVFrame* frame);
188 /// start thread to load the video file/capture/stream
206 ListBase m_frameCacheBase; // list of frames that are ready
207 ListBase m_frameCacheFree; // list of frames that are unused
208 ListBase m_packetCacheBase; // list of packets that are ready for decoding
209 ListBase m_packetCacheFree; // list of packets that are unused
210 pthread_mutex_t m_cacheMutex;
212 AVFrame *allocFrameRGB();
213 static void *cacheThread(void *);
216 inline VideoFFmpeg * getFFmpeg (PyImage * self)
218 return static_cast<VideoFFmpeg*>(self->m_image);