Basic Image AlgorithmS Library 2.8.0

VideoSink_FFmpeg.hh

00001 #ifndef __VIDEOSINK_FFMPEG_HH__
00002 #define __VIDEOSINK_FFMPEG_HH__
00003 
00004 #include <Base/Image/Image.hh>
00005 
00006 // Check for FFmpeg libraries in BIAS
00007 #ifndef BIAS_HAVE_FFMPEG
00008 //#error BIAS was build without FFmpeg, please enable USE_FFMPEG and recompile BIAS.
00009 #endif
00010 
00011 // Fix for FFmpeg on 32 bit systems
00012 #ifndef INT64_C
00013 #define INT64_C(c) (c ## LL)
00014 #define UINT64_C(c) (c ## ULL)
00015 #endif
00016 
00017 #ifdef __cplusplus
00018 extern "C" {
00019 #endif
00020 #include <libavcodec/avcodec.h>
00021 #include <libavformat/avformat.h>
00022 #include <libavutil/avstring.h>
00023 #include <libswscale/swscale.h>
00024 #ifdef __cplusplus
00025 }
00026 #endif
00027 
00028 namespace BIAS
00029 {
00030   /**
00031    * @class VideoSink_FFmpeg
00032    * @author Carsten Heine <carstenheine@gmx.de>
00033    * @brief Video encoding using FFmpeg library.
00034    *
00035    * Supports realtime encoding.
00036    *
00037    * Usage:
00038    *
00039    * @code
00040    * // Create a video sink object
00041    * VideoSink_FFmpeg *vs;
00042    * vs = new VideoSink_FFmpeg;
00043    *
00044    * // Set some parameters
00045    * vs->SetSize(640, 480);
00046    * vs->SetEncoder(CODEC_ID_MPEG4);
00047    * vs->SetFPS(25.0);
00048    * vs->SetBitrate(600000);
00049    *
00050    * // Open output video file and write header
00051    * vs->Open("video.mkv");
00052    *
00053    * // Add some images to the file
00054    * for (int i=0; i<250; i++)
00055    *   vs->AddFrame(image[i]);
00056    *
00057    * // Write trailer and close the file
00058    * vs->Close
00059    * @endcode
00060    *
00061    * For more information about supported codecs and formats visit:
00062    * http://www.ffmpeg.org
00063    */
00064   class VideoSink_FFmpeg
00065   {
00066   public:
00067 
00068     /**
00069      * Standard constructor.
00070      */
00071     VideoSink_FFmpeg();
00072 
00073     /**
00074      * Clean up.
00075      */
00076     ~VideoSink_FFmpeg();
00077 
00078     /**
00079      * Reset all user parameters to default values.
00080      */
00081     void SetDefaults();
00082 
00083     /**
00084      * Set encoder. Tested with CODEC_ID_MPEG4, CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO,
00085      * CODEC_ID_FFV1 and some others. This has to be called before Open.
00086      */
00087     void SetEncoder(enum CodecID codecId);
00088 
00089     /**
00090      * Set frame size of video. Resolution has to be a multiple of two. This
00091      * has to be called before Open.
00092      */
00093     void SetSize(int width, int height);
00094 
00095     /**
00096      * Set framerate in frames per second. Not all encoders support all
00097      * framerates. For Mpeg1 and Mpeg2 this will try to get the best alternative,
00098      * if you specify an invalid value. Check Google if you have problems setting
00099      * a valid framerate. This has to be called before Open.
00100      */
00101     void SetFPS(float fps);
00102 
00103     /**
00104      * Set bitrate in bits per second. This has to be called before Open.
00105      */
00106     void SetBitrate(int bitrate);
00107 
00108     /**
00109      * Set the gop size of the video stream. This is the number of frames
00110      * between fully coded intra frames. Set this to -1 for intra only. Will
00111      * be ignored for encoders, that only support intra frames. This has to be
00112      * called before Open.
00113      */
00114     void SetGopSize(int gopSize);
00115 
00116     /**
00117      * Set maximal number of bidirectional frames. Set this to 0 for no B frames.
00118      * Will be ignored for encoders, that don't support B-frames. This has to be
00119      * called before Open.
00120      */
00121     void SetMaxBFrames(int maxBFrames);
00122 
00123     /**
00124      * Set realtime mode. This will set the presentation timestamps of the frames
00125      * based on the real time clock for live encoding. This will adjust different
00126      * time deltas between frames for playback in correct speed. This is only
00127      * supported for .mkv and .flv files at the moment. This has to be called
00128      * before Open.
00129      */
00130     void SetRealtime(bool realtime);
00131 
00132     /**
00133      * Set if the output file should be overridden if it already exists. This
00134      * has to be called before Open.
00135      */
00136     void SetOverride(bool override);
00137 
00138     /**
00139      * Open a file for video output and init the codec. The output format will
00140      * be determined by the file extension. Possible values are .mkv (Matroska),
00141      * .mpg (Mpeg), .mp4 (Mpeg4), .avi (Avi) and many others. The parameters set by
00142      * the user will be checked at this point. If another file is already opened for
00143      * output, it will be closed first. Setting any parameters after the file is
00144      * opened will have no effect.
00145      * @returns 0 on success, -1 on error.
00146      */
00147     int Open(std::string filename);
00148 
00149     /**
00150      * Close the video output file, write remaining frames and clean up. Parameters
00151      * set by the user will not be overridden, so you can encode another video with
00152      * the same settings. To reset all parameters call SetDefaults.
00153      * @returns 0 on success, -1 on error.
00154      */
00155     int Close();
00156 
00157     /**
00158      * Add an Image to the stream and write it to the video output file. Resampling
00159      * will be done automatically.
00160      * @returns 0 on success, -1 on error, 1 on non critical error.
00161      */
00162     int AddFrame(const ImageBase& image);
00163 
00164     /**
00165      * Load an Image from file and add it to the video output file.
00166      * @returns 0 on success, -1 on error, 1 on non critical error.
00167      */
00168     int AddFrame(std::string filename);
00169 
00170     /**
00171      * Load each Image from a list of files and add them to the video output file.
00172      * @returns 0 on success, -1 on error, 1 on non critical error.
00173      */
00174     int AddFrames(std::vector<std::string> filenames);
00175 
00176     /**
00177      * Get status of video output file.
00178      */
00179     inline bool IsOpened() { return pFormatCtx_; }
00180 
00181     /**
00182      * Get the last error message.
00183      */
00184     inline std::string GetError() { return errMsg_; }
00185 
00186   protected:
00187 
00188     /**
00189      * Called by constructor to init variables.
00190      */
00191     void Init_();
00192 
00193     /**
00194      * Get the pixel format from a given color model.
00195      * @returns pixel format or PIX_FMT_NONE if not found.
00196      */
00197     enum PixelFormat GetPixelFormat_(enum ImageBase::EColorModel colorModel);
00198 
00199     enum CodecID    codecId_;      ///< Codec ID (default: CODEC_ID_MPEG1VIDEO)
00200     int             width_;        ///< Frame width (default: 352)
00201     int             height_;       ///< Frame height (default: 288)
00202     AVRational      fps_;          ///< Framerate (default: 25)
00203     int             bitrate_;      ///< Bitrate (default: 1200000)
00204     int             gopSize_;      ///< Gop size (default: 12)
00205     int             maxBFrames_;   ///< Max B-frames (default: 0)
00206     bool            realtime_;     ///< Realtime flag (default: false)
00207     bool            override_;     ///< Override flag (default: true)
00208 
00209     AVFormatContext *pFormatCtx_;  ///< Format context for video encoder
00210     AVFrame         *pInputPict_;  ///< Input picture
00211     SwsContext      *pSwsCtx_;     ///< Color conversion context
00212     AVFrame         *pPict_;       ///< Output picture
00213     uint8_t         *pPictBuffer_; ///< Output picture buffer
00214     uint8_t         *pBuffer_;     ///< Encoding buffer
00215     uint64_t        bufferSize_;   ///< Encoding buffer size
00216     std::string     errMsg_;       ///< The last error message
00217     int64_t         pts_;          ///< Presentation timestamp
00218   };
00219 }
00220 
00221 #endif // __VIDEOSINK_FFMPEG_HH__
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends