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 -----------------------------------------------------------------------------
22 #if !defined VIDEOFFMPEG_H
27 #include <ffmpeg/avformat.h>
28 #include <ffmpeg/avcodec.h>
29 #include <ffmpeg/rational.h>
30 #include <ffmpeg/swscale.h>
33 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
34 #define FFMPEG_OLD_FRAME_RATE 1
36 #define FFMPEG_CODEC_IS_POINTER 1
39 #if LIBAVFORMAT_VERSION_INT >= (52 << 16)
40 #define FFMPEG_PB_IS_POINTER 1
43 #ifdef FFMPEG_CODEC_IS_POINTER
44 static inline AVCodecContext* get_codec_from_stream(AVStream* stream)
49 static inline AVCodecContext* get_codec_from_stream(AVStream* stream)
51 return &stream->codec;
55 #include "VideoBase.h"
58 // type VideoFFmpeg declaration
59 class VideoFFmpeg : public VideoBase
63 VideoFFmpeg (HRESULT * hRslt);
65 virtual ~VideoFFmpeg ();
67 /// set initial parameters
68 void initParams (short width, short height, float rate, bool image=false);
69 /// open video/image file
70 virtual void openFile (char * file);
71 /// open video capture device
72 virtual void openCam (char * driver, short camIdx);
74 /// release video source
75 virtual bool release (void);
78 virtual bool play (void);
80 virtual bool stop (void);
83 virtual void setRange (double start, double stop);
85 virtual void setFrameRate (float rate);
86 // some specific getters and setters
87 int getPreseek(void) { return m_preseek; }
88 void setPreseek(int preseek) { if (preseek >= 0) m_preseek = preseek; }
89 bool getDeinterlace(void) { return m_deinterlace; }
90 void setDeinterlace(bool deinterlace) { m_deinterlace = deinterlace; }
91 char *getImageName(void) { return (m_isImage) ? m_imageName.Ptr() : NULL; }
95 // format and codec information
97 AVFormatContext *m_formatCtx;
98 AVCodecContext *m_codecCtx;
99 // raw frame extracted from video file
101 // deinterlaced frame if codec requires it
102 AVFrame *m_frameDeinterlaced;
103 // decoded RGB24 frame if codec requires it
105 // conversion from raw to RGB is done with sws_scale
106 struct SwsContext *m_imgConvertCtx;
107 // should the codec be deinterlaced?
109 // number of frame of preseek
111 // order number of stream holding the video in format context
114 // the actual frame rate
115 double m_baseFrameRate;
117 /// last displayed frame
120 /// current file pointer position in file expressed in frame number
123 /// time of video play start
126 /// width of capture in pixel
129 /// height of capture in pixel
132 /// frame rate of capture in frames per seconds
135 /// is file an image?
138 /// keep last image name
139 STR_String m_imageName;
141 /// image calculation
142 virtual void calcImage (unsigned int texId);
144 /// load frame from video
145 void loadFrame (void);
147 /// set actual position
148 void setPositions (void);
150 /// get actual framerate
151 double actFrameRate (void) { return m_frameRate * m_baseFrameRate; }
153 /// common function to video file and capture
154 int openStream(const char *filename, AVInputFormat *inputFormat, AVFormatParameters *formatParams);
156 /// check if a frame is available and load it in pFrame, return true if a frame could be retrieved
157 bool grabFrame(long frame);
159 /// return the frame in RGB24 format, the image data is found in AVFrame.data[0]
160 AVFrame* getFrame(void) { return m_frameRGB; }
163 inline VideoFFmpeg * getFFmpeg (PyImage * self)
165 return static_cast<VideoFFmpeg*>(self->m_image);