TuttleOFX
1
|
00001 #ifndef _TUTTLE_PLUGIN_LIBAV_LIBAVVIDEOREADER_HPP_ 00002 #define _TUTTLE_PLUGIN_LIBAV_LIBAVVIDEOREADER_HPP_ 00003 00004 #include "LibAV.hpp" 00005 00006 #include <boost/lexical_cast.hpp> 00007 #include <boost/cstdint.hpp> 00008 00009 #include <iostream> 00010 #include <string> 00011 #include <vector> 00012 00013 namespace tuttle { 00014 namespace plugin { 00015 namespace av { 00016 00017 enum EIntrelacment { eInterlacmentNone, eInterlacmentUpper, eInterlacmentLower }; 00018 00019 class LibAVVideoReader : public LibAV 00020 { 00021 public: 00022 explicit LibAVVideoReader(); 00023 ~LibAVVideoReader(); 00024 00025 bool open( const std::string& filename ); 00026 void close(); 00027 bool read( const int frame ); 00028 00029 private: 00030 bool setupStreamInfo(); 00031 00032 bool hasVideo() const 00033 { 00034 return !_videoIdx.empty(); 00035 } 00036 00037 AVStream* getVideoStream() 00038 { 00039 return _avFormatOptions && _currVideoIdx >= 0 ? _avFormatOptions->streams[_videoIdx[_currVideoIdx]] : NULL; 00040 } 00041 00042 void openVideoCodec(); 00043 void closeVideoCodec(); 00044 boost::int64_t getTimeStamp( const int pos ) const; 00045 00046 /** 00047 * @brief Seek to the nearest previous keyframe from pos. 00048 * Write in _data the image result. 00049 * @param pos frame number to seek 00050 */ 00051 bool seek( size_t pos ); 00052 /** 00053 * @brief Decode the current frame 00054 * @param the number of the current frame 00055 */ 00056 bool decodeImage( const int frame ); 00057 00058 public: 00059 int width() const 00060 { 00061 return _width; 00062 } 00063 00064 int height() const 00065 { 00066 return _height; 00067 } 00068 00069 double aspectRatio() const 00070 { 00071 return _aspect; 00072 } 00073 00074 EIntrelacment interlacment() 00075 { 00076 return _interlacment; 00077 } 00078 00079 int bitRate() const 00080 { 00081 return _bitRate; 00082 } 00083 00084 double fps() const 00085 { 00086 if( _fpsDen ) 00087 { 00088 return _fpsNum / (double) _fpsDen; 00089 } 00090 return 1.0f; 00091 } 00092 00093 uint64_t nbFrames( ) const 00094 { 00095 return _nbFrames; 00096 } 00097 00098 int frame( ) const 00099 { 00100 return _lastDecodedPos; 00101 } 00102 00103 bool isOpen() const 00104 { 00105 return _isOpen; 00106 } 00107 00108 unsigned char* data() 00109 { 00110 return &_data[0]; 00111 } 00112 00113 std::string codecName( ) const 00114 { 00115 if( !_videoCodec ) 00116 return ""; 00117 return ( _videoCodec->long_name ) + std::string( " (" ) + std::string( _videoCodec->name ) + std::string( ", " ) + boost::lexical_cast<std::string>( static_cast<int>( _videoCodec->id ) ) + std::string( ")" ); 00118 } 00119 00120 std::string formatName( ) const 00121 { 00122 if( !_format ) 00123 return ""; 00124 return ( _format->long_name ) + std::string( " (" ) + std::string( _format->name ) + std::string( ")" ); 00125 } 00126 00127 std::string codecIDString( ) const 00128 { 00129 if( !_videoCodec ) 00130 return ""; 00131 return _videoCodec->name; 00132 } 00133 00134 std::string codecTypeString( ) const 00135 { 00136 if( !_videoCodec ) 00137 return ""; 00138 return codecType_toString( _videoCodec->type ); 00139 } 00140 00141 // PixelFormat pixelFormat( ) const 00142 // { 00143 // if( !_videoCodec || !_avctxOptions ||_videoCodec->type <= 0 || !_avctxOptions[_videoCodec->type] ) 00144 // return PIX_FMT_NONE; 00145 // return _avctxOptions[_videoCodec->type]->pix_fmt; 00146 // } 00147 00148 // std::string pixelFormatString( ) const 00149 // { 00150 // return pixelFormat_toString( pixelFormat() ); 00151 // } 00152 00153 public: // private: 00154 AVFormatContext* _avFormatOptions; 00155 AVStream* _stream; 00156 00157 AVInputFormat* _format; 00158 AVFrame* _avFrame; 00159 AVCodec* _videoCodec; 00160 AVPacket _pkt; 00161 00162 struct SwsContext* _sws_context; ///< contexte de transformation swscale 00163 std::vector<int> _videoIdx; 00164 int _fpsNum; 00165 int _fpsDen; 00166 int _currVideoIdx; 00167 uint64_t _nbFrames; 00168 int _width; 00169 int _height; 00170 double _aspect; 00171 int _bitRate; 00172 std::vector<unsigned char> _data; 00173 bool _offsetTime; 00174 int _lastSearchPos; 00175 int _lastDecodedPos; 00176 int _lastDecodedFrame; 00177 bool _isOpen; 00178 EIntrelacment _interlacment; 00179 }; 00180 00181 } 00182 } 00183 } 00184 00185 #endif