Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_FFmpeg.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __VIDEOSOURCE_FFMPEG_HH__
26 #define __VIDEOSOURCE_FFMPEG_HH__
27 
28 #include <VideoSource/VideoSource_Base.hh>
29 #include <Image/Camera.hh>
30 
31 // Check for FFmpeg libraries in BIAS
32 #ifndef BIAS_HAVE_FFMPEG
33  #error BIAS was build without FFmpeg, please enable USE_FFMPEG and recompile BIAS.
34 #endif
35 
36 // Fix for FFmpeg on 32 bit systems
37 #ifndef INT64_C
38 #define INT64_C(c) (c ## LL)
39 #define UINT64_C(c) (c ## ULL)
40 #endif
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 #include <libavcodec/avcodec.h>
46 #include <libavformat/avformat.h>
47 #include <libswscale/swscale.h>
48 #ifdef __cplusplus
49 }
50 #endif
51 
52 namespace BIAS {
53  /**
54  * @class VideoSource_FFmpeg
55  * @author Carsten Heine <carstenheine@gmx.de>
56  * @ingroup g_videosource
57  * @brief Simple video decoding of video files using FFmpeg library.
58  */
59  class BIASVideoSource_EXPORT VideoSource_FFmpeg : public VideoSource
60  {
61  public:
62 
63  /**
64  * @brief Standard constructor.
65  */
67 
68  /**
69  * @brief Clean up.
70  */
71  virtual ~VideoSource_FFmpeg();
72 
73  /**
74  * @brief Open video file.
75  * @return 0 on success, -1 on error
76  */
77  virtual int OpenDevice(const char *filename);
78 
79  /**
80  * @brief Close video file.
81  * @return 0 on success, -1 on error
82  */
83  virtual int CloseDevice();
84 
85  /**
86  * @brief Get the next frame from the video file. The number of the frame #[num] and
87  * presentation time in microseconds #[pts] are stored in the metadata of the
88  * image for later use.
89  * @return 0 on success, -1 on error
90  */
91  virtual int GrabSingle(Camera<unsigned char> &image);
92 
93  /**
94  * @brief Seek to frame in video stream.
95  * @return 0 on success, -1 on error
96  */
97  int SeekFrame(int64_t frame);
98 
99  /**
100  * @brief Seek to time in milliseconds in video stream.
101  * @return 0 on success, -1 on error
102  */
103  int SeekMs(int ms);
104 
105  /**
106  * @brief Set output color model. Not all color models are supported by all decoders.
107  * @return 0 on success, -1 on error
108  */
109  virtual int SetColorModel(ImageBase::EColorModel mode);
110 
111  /**
112  * @brief Get the framerate of the video stream.
113  */
114  inline float GetFPS() { return FramesPerSecond_; }
115 
116  /**
117  * @brief Get the number of frames in the video stream. 0 if unknown.
118  */
119  inline int64_t GetNumberOfFrames() const { return numFrames_; }
120 
121  /**
122  * @brief Get the number of frames decoded so far.
123  */
124  inline int64_t GetCurrentFrameNumber() const { return frameNumber_; }
125 
126  /**
127  * @brief Get the timebase numerator of the video stream.
128  */
129  inline int GetTimeBaseNum() { return timeBaseNum_; }
130 
131  /**
132  * @brief Get the timebase denumerator of the video stream.
133  */
134  inline int GetTimeBaseDen() { return timeBaseDen_; }
135 
136  /**
137  * @brief Get the timestamp of the video stream in microseconds in Unix time.
138  */
139  inline int64_t GetTimestamp() { return timestamp_; }
140 
141  /**
142  * @brief Get a string representation of the timestamp of the video stream.
143  */
144  inline char * GetDateString() { return dateStr_; }
145 
146  /**
147  * @brief Get the duration of the video stream in microseconds.
148  */
149  inline int64_t GetDuration() { return duration_; }
150 
151  /**
152  * @brief Get the presentation timestamp of the last decoded frame in microseconds.
153  */
154  inline int64_t GetPTS() { return pts_; }
155 
156  /**
157  * @brief Get the last error message.
158  */
159  inline std::string GetError() { return errMsg_; }
160 
161  protected:
162 
163  /**
164  * Called by constructor to init variables.
165  */
166  void Init_();
167 
168  /**
169  * Try to extract date string of format "YYYYMMDD_HHMMSS" from string.
170  */
171  char * ExtractDateString_(std::string str);
172 
173  AVFormatContext *pFormatCtx_; ///< Format context for video decoder
174  int streamIndex_; ///< Index of the video stream
175  AVFrame *pFrame_; ///< Video stream frame
176  AVFrame *pOutputFrame_; ///< Output frame
177  uint8_t *pBuffer_; ///< Ouput frame buffer
178  enum PixelFormat outputPixFmt_; ///< Output frame pixel format
179  SwsContext *pSwsCtx_; ///< Color conversion context
180  int64_t numFrames_; ///< Number of frames of the stream, 0 if unknown
181  int64_t frameNumber_; ///< Number of decoded frames
182  int timeBaseNum_; ///< Stream timebase numerator
183  int timeBaseDen_; ///< Stream timebase denumerator
184  int64_t timestamp_; ///< Stream timestamp
185  char *dateStr_; ///< String representation of timestamp_
186  int64_t duration_; ///< Stream duration in milliseconds, 0 if unknown
187  int64_t pts_; ///< Presentation timestamp of last decoded frame in microseconds
188  std::string errMsg_; ///< The last error message
189  };
190 }
191 
192 #endif // __VIDEOSOURCE_FFMPEG_HH__
int64_t duration_
Stream duration in milliseconds, 0 if unknown.
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
int64_t pts_
Presentation timestamp of last decoded frame in microseconds.
Defines a common interface to different devices.
Simple video decoding of video files using FFmpeg library.
int streamIndex_
Index of the video stream.
std::string errMsg_
The last error message.
int64_t GetNumberOfFrames() const
Get the number of frames in the video stream.
int GetTimeBaseDen()
Get the timebase denumerator of the video stream.
int GetTimeBaseNum()
Get the timebase numerator of the video stream.
AVFrame * pFrame_
Video stream frame.
int64_t numFrames_
Number of frames of the stream, 0 if unknown.
std::string GetError()
Get the last error message.
int64_t GetPTS()
Get the presentation timestamp of the last decoded frame in microseconds.
int timeBaseNum_
Stream timebase numerator.
AVFormatContext * pFormatCtx_
Format context for video decoder.
SwsContext * pSwsCtx_
Color conversion context.
int64_t GetDuration()
Get the duration of the video stream in microseconds.
char * GetDateString()
Get a string representation of the timestamp of the video stream.
int64_t frameNumber_
Number of decoded frames.
int timeBaseDen_
Stream timebase denumerator.
int64_t GetCurrentFrameNumber() const
Get the number of frames decoded so far.
char * dateStr_
String representation of timestamp_.
int64_t timestamp_
Stream timestamp.
float GetFPS()
Get the framerate of the video stream.
uint8_t * pBuffer_
Ouput frame buffer.
int64_t GetTimestamp()
Get the timestamp of the video stream in microseconds in Unix time.
AVFrame * pOutputFrame_
Output frame.