Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSink_FFmpeg.hh
1 #ifndef __VIDEOSINK_FFMPEG_HH__
2 #define __VIDEOSINK_FFMPEG_HH__
3 
4 #include <Base/Image/Image.hh>
5 
6 // Check for FFmpeg libraries in BIAS
7 #ifndef BIAS_HAVE_FFMPEG
8 //#error BIAS was build without FFmpeg, please enable USE_FFMPEG and recompile BIAS.
9 #endif
10 
11 // Fix for FFmpeg on 32 bit systems
12 #ifndef INT64_C
13 #define INT64_C(c) (c ## LL)
14 #define UINT64_C(c) (c ## ULL)
15 #endif
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 #include <libavcodec/avcodec.h>
21 #include <libavformat/avformat.h>
22 #include <libavutil/avstring.h>
23 #include <libswscale/swscale.h>
24 #ifdef __cplusplus
25 }
26 #endif
27 
28 namespace BIAS
29 {
30  /**
31  * @class VideoSink_FFmpeg
32  * @author Carsten Heine <carstenheine@gmx.de>
33  * @brief Video encoding using FFmpeg library.
34  *
35  * Supports realtime encoding.
36  *
37  * Usage:
38  *
39  * @code
40  * // Create a video sink object
41  * VideoSink_FFmpeg *vs;
42  * vs = new VideoSink_FFmpeg;
43  *
44  * // Set some parameters
45  * vs->SetSize(640, 480);
46  * vs->SetEncoder(CODEC_ID_MPEG4);
47  * vs->SetFPS(25.0);
48  * vs->SetBitrate(600000);
49  *
50  * // Open output video file and write header
51  * vs->Open("video.mkv");
52  *
53  * // Add some images to the file
54  * for (int i=0; i<250; i++)
55  * vs->AddFrame(image[i]);
56  *
57  * // Write trailer and close the file
58  * vs->Close
59  * @endcode
60  *
61  * For more information about supported codecs and formats visit:
62  * http://www.ffmpeg.org
63  */
65  {
66  public:
67 
68  /**
69  * Standard constructor.
70  */
72 
73  /**
74  * Clean up.
75  */
77 
78  /**
79  * Reset all user parameters to default values.
80  */
81  void SetDefaults();
82 
83  /**
84  * Set encoder. Tested with CODEC_ID_MPEG4, CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO,
85  * CODEC_ID_FFV1 and some others. This has to be called before Open.
86  */
87  void SetEncoder(enum CodecID codecId);
88 
89  /**
90  * Set frame size of video. Resolution has to be a multiple of two. This
91  * has to be called before Open.
92  */
93  void SetSize(int width, int height);
94 
95  /**
96  * Set framerate in frames per second. Not all encoders support all
97  * framerates. For Mpeg1 and Mpeg2 this will try to get the best alternative,
98  * if you specify an invalid value. Check Google if you have problems setting
99  * a valid framerate. This has to be called before Open.
100  */
101  void SetFPS(float fps);
102 
103  /**
104  * Set bitrate in bits per second. This has to be called before Open.
105  */
106  void SetBitrate(int bitrate);
107 
108  /**
109  * Set the gop size of the video stream. This is the number of frames
110  * between fully coded intra frames. Set this to -1 for intra only. Will
111  * be ignored for encoders, that only support intra frames. This has to be
112  * called before Open.
113  */
114  void SetGopSize(int gopSize);
115 
116  /**
117  * Set maximal number of bidirectional frames. Set this to 0 for no B frames.
118  * Will be ignored for encoders, that don't support B-frames. This has to be
119  * called before Open.
120  */
121  void SetMaxBFrames(int maxBFrames);
122 
123  /**
124  * Set realtime mode. This will set the presentation timestamps of the frames
125  * based on the real time clock for live encoding. This will adjust different
126  * time deltas between frames for playback in correct speed. This is only
127  * supported for .mkv and .flv files at the moment. This has to be called
128  * before Open.
129  */
130  void SetRealtime(bool realtime);
131 
132  /**
133  * Set if the output file should be overridden if it already exists. This
134  * has to be called before Open.
135  */
136  void SetOverride(bool override);
137 
138  /**
139  * Open a file for video output and init the codec. The output format will
140  * be determined by the file extension. Possible values are .mkv (Matroska),
141  * .mpg (Mpeg), .mp4 (Mpeg4), .avi (Avi) and many others. The parameters set by
142  * the user will be checked at this point. If another file is already opened for
143  * output, it will be closed first. Setting any parameters after the file is
144  * opened will have no effect.
145  * @returns 0 on success, -1 on error.
146  */
147  int Open(std::string filename);
148 
149  /**
150  * Close the video output file, write remaining frames and clean up. Parameters
151  * set by the user will not be overridden, so you can encode another video with
152  * the same settings. To reset all parameters call SetDefaults.
153  * @returns 0 on success, -1 on error.
154  */
155  int Close();
156 
157  /**
158  * Add an Image to the stream and write it to the video output file. Resampling
159  * will be done automatically.
160  * @returns 0 on success, -1 on error, 1 on non critical error.
161  */
162  int AddFrame(const ImageBase& image);
163 
164  /**
165  * Load an Image from file and add it to the video output file.
166  * @returns 0 on success, -1 on error, 1 on non critical error.
167  */
168  int AddFrame(std::string filename);
169 
170  /**
171  * Load each Image from a list of files and add them to the video output file.
172  * @returns 0 on success, -1 on error, 1 on non critical error.
173  */
174  int AddFrames(std::vector<std::string> filenames);
175 
176  /**
177  * Get status of video output file.
178  */
179  inline bool IsOpened() { return pFormatCtx_; }
180 
181  /**
182  * Get the last error message.
183  */
184  inline std::string GetError() { return errMsg_; }
185 
186  protected:
187 
188  /**
189  * Called by constructor to init variables.
190  */
191  void Init_();
192 
193  /**
194  * Get the pixel format from a given color model.
195  * @returns pixel format or PIX_FMT_NONE if not found.
196  */
197  enum PixelFormat GetPixelFormat_(enum ImageBase::EColorModel colorModel);
198 
199  enum CodecID codecId_; ///< Codec ID (default: CODEC_ID_MPEG1VIDEO)
200  int width_; ///< Frame width (default: 352)
201  int height_; ///< Frame height (default: 288)
202  AVRational fps_; ///< Framerate (default: 25)
203  int bitrate_; ///< Bitrate (default: 1200000)
204  int gopSize_; ///< Gop size (default: 12)
205  int maxBFrames_; ///< Max B-frames (default: 0)
206  bool realtime_; ///< Realtime flag (default: false)
207  bool override_; ///< Override flag (default: true)
208 
209  AVFormatContext *pFormatCtx_; ///< Format context for video encoder
210  AVFrame *pInputPict_; ///< Input picture
211  SwsContext *pSwsCtx_; ///< Color conversion context
212  AVFrame *pPict_; ///< Output picture
213  uint8_t *pPictBuffer_; ///< Output picture buffer
214  uint8_t *pBuffer_; ///< Encoding buffer
215  uint64_t bufferSize_; ///< Encoding buffer size
216  std::string errMsg_; ///< The last error message
217  int64_t pts_; ///< Presentation timestamp
218  };
219 }
220 
221 #endif // __VIDEOSINK_FFMPEG_HH__
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
void SetEncoder(enum CodecID codecId)
Set encoder.
void SetSize(int width, int height)
Set frame size of video.
AVFormatContext * pFormatCtx_
Format context for video encoder.
void SetRealtime(bool realtime)
Set realtime mode.
bool override_
Override flag (default: true)
uint64_t bufferSize_
Encoding buffer size.
int maxBFrames_
Max B-frames (default: 0)
void Init_()
Called by constructor to init variables.
Video encoding using FFmpeg library.
void SetFPS(float fps)
Set framerate in frames per second.
int AddFrames(std::vector< std::string > filenames)
Load each Image from a list of files and add them to the video output file.
int Close()
Close the video output file, write remaining frames and clean up.
void SetMaxBFrames(int maxBFrames)
Set maximal number of bidirectional frames.
int AddFrame(const ImageBase &image)
Add an Image to the stream and write it to the video output file.
enum PixelFormat GetPixelFormat_(enum ImageBase::EColorModel colorModel)
Get the pixel format from a given color model.
void SetGopSize(int gopSize)
Set the gop size of the video stream.
uint8_t * pPictBuffer_
Output picture buffer.
enum CodecID codecId_
Codec ID (default: CODEC_ID_MPEG1VIDEO)
int height_
Frame height (default: 288)
std::string errMsg_
The last error message.
void SetDefaults()
Reset all user parameters to default values.
SwsContext * pSwsCtx_
Color conversion context.
AVFrame * pInputPict_
Input picture.
int width_
Frame width (default: 352)
AVFrame * pPict_
Output picture.
AVRational fps_
Framerate (default: 25)
bool realtime_
Realtime flag (default: false)
int gopSize_
Gop size (default: 12)
void SetBitrate(int bitrate)
Set bitrate in bits per second.
int64_t pts_
Presentation timestamp.
int Open(std::string filename)
Open a file for video output and init the codec.
bool IsOpened()
Get status of video output file.
int bitrate_
Bitrate (default: 1200000)
VideoSink_FFmpeg()
Standard constructor.
uint8_t * pBuffer_
Encoding buffer.
std::string GetError()
Get the last error message.
void SetOverride(bool override)
Set if the output file should be overridden if it already exists.
This is the base class for images in BIAS.
Definition: ImageBase.hh:102