gpucodec module for video decoding/encoding
This commit is contained in:
@@ -39,19 +39,6 @@ if(HAVE_CUDA)
|
||||
ocv_cuda_compile(cuda_objs ${lib_cuda} ${ncv_cuda})
|
||||
|
||||
set(cuda_link_libs ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
|
||||
|
||||
if(WITH_NVCUVID)
|
||||
set(cuda_link_libs ${cuda_link_libs} ${CUDA_nvcuvid_LIBRARY})
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
find_cuda_helper_libs(nvcuvenc)
|
||||
set(cuda_link_libs ${cuda_link_libs} ${CUDA_nvcuvenc_LIBRARY})
|
||||
endif()
|
||||
|
||||
if(WITH_FFMPEG)
|
||||
set(cuda_link_libs ${cuda_link_libs} ${HIGHGUI_LIBRARIES})
|
||||
endif()
|
||||
else()
|
||||
set(lib_cuda "")
|
||||
set(cuda_objs "")
|
||||
|
@@ -687,453 +687,6 @@ Releases all inner buffer's memory.
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU
|
||||
---------------------
|
||||
Video writer class.
|
||||
|
||||
.. ocv:class:: gpu::VideoWriter_GPU
|
||||
|
||||
The class uses H264 video codec.
|
||||
|
||||
.. note:: Currently only Windows platform is supported.
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::VideoWriter_GPU
|
||||
-------------------------------------
|
||||
Constructors.
|
||||
|
||||
.. ocv:function:: gpu::VideoWriter_GPU::VideoWriter_GPU()
|
||||
.. ocv:function:: gpu::VideoWriter_GPU::VideoWriter_GPU(const String& fileName, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR)
|
||||
.. ocv:function:: gpu::VideoWriter_GPU::VideoWriter_GPU(const String& fileName, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR)
|
||||
.. ocv:function:: gpu::VideoWriter_GPU::VideoWriter_GPU(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR)
|
||||
.. ocv:function:: gpu::VideoWriter_GPU::VideoWriter_GPU(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR)
|
||||
|
||||
:param fileName: Name of the output video file. Only AVI file format is supported.
|
||||
|
||||
:param frameSize: Size of the input video frames.
|
||||
|
||||
:param fps: Framerate of the created video stream.
|
||||
|
||||
:param params: Encoder parameters. See :ocv:struct:`gpu::VideoWriter_GPU::EncoderParams` .
|
||||
|
||||
:param format: Surface format of input frames ( ``SF_UYVY`` , ``SF_YUY2`` , ``SF_YV12`` , ``SF_NV12`` , ``SF_IYUV`` , ``SF_BGR`` or ``SF_GRAY``). BGR or gray frames will be converted to YV12 format before encoding, frames with other formats will be used as is.
|
||||
|
||||
:param encoderCallback: Callbacks for video encoder. See :ocv:class:`gpu::VideoWriter_GPU::EncoderCallBack` . Use it if you want to work with raw video stream.
|
||||
|
||||
The constructors initialize video writer. FFMPEG is used to write videos. User can implement own multiplexing with :ocv:class:`gpu::VideoWriter_GPU::EncoderCallBack` .
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::open
|
||||
--------------------------
|
||||
Initializes or reinitializes video writer.
|
||||
|
||||
.. ocv:function:: void gpu::VideoWriter_GPU::open(const String& fileName, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR)
|
||||
.. ocv:function:: void gpu::VideoWriter_GPU::open(const String& fileName, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR)
|
||||
.. ocv:function:: void gpu::VideoWriter_GPU::open(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR)
|
||||
.. ocv:function:: void gpu::VideoWriter_GPU::open(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR)
|
||||
|
||||
The method opens video writer. Parameters are the same as in the constructor :ocv:func:`gpu::VideoWriter_GPU::VideoWriter_GPU` . The method throws :ocv:class:`Exception` if error occurs.
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::isOpened
|
||||
------------------------------
|
||||
Returns true if video writer has been successfully initialized.
|
||||
|
||||
.. ocv:function:: bool gpu::VideoWriter_GPU::isOpened() const
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::close
|
||||
---------------------------
|
||||
Releases the video writer.
|
||||
|
||||
.. ocv:function:: void gpu::VideoWriter_GPU::close()
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::write
|
||||
---------------------------
|
||||
Writes the next video frame.
|
||||
|
||||
.. ocv:function:: void gpu::VideoWriter_GPU::write(const cv::gpu::GpuMat& image, bool lastFrame = false)
|
||||
|
||||
:param image: The written frame.
|
||||
|
||||
:param lastFrame: Indicates that it is end of stream. The parameter can be ignored.
|
||||
|
||||
The method write the specified image to video file. The image must have the same size and the same surface format as has been specified when opening the video writer.
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::EncoderParams
|
||||
-----------------------------------
|
||||
.. ocv:struct:: gpu::VideoWriter_GPU::EncoderParams
|
||||
|
||||
Different parameters for CUDA video encoder. ::
|
||||
|
||||
struct EncoderParams
|
||||
{
|
||||
int P_Interval; // NVVE_P_INTERVAL,
|
||||
int IDR_Period; // NVVE_IDR_PERIOD,
|
||||
int DynamicGOP; // NVVE_DYNAMIC_GOP,
|
||||
int RCType; // NVVE_RC_TYPE,
|
||||
int AvgBitrate; // NVVE_AVG_BITRATE,
|
||||
int PeakBitrate; // NVVE_PEAK_BITRATE,
|
||||
int QP_Level_Intra; // NVVE_QP_LEVEL_INTRA,
|
||||
int QP_Level_InterP; // NVVE_QP_LEVEL_INTER_P,
|
||||
int QP_Level_InterB; // NVVE_QP_LEVEL_INTER_B,
|
||||
int DeblockMode; // NVVE_DEBLOCK_MODE,
|
||||
int ProfileLevel; // NVVE_PROFILE_LEVEL,
|
||||
int ForceIntra; // NVVE_FORCE_INTRA,
|
||||
int ForceIDR; // NVVE_FORCE_IDR,
|
||||
int ClearStat; // NVVE_CLEAR_STAT,
|
||||
int DIMode; // NVVE_SET_DEINTERLACE,
|
||||
int Presets; // NVVE_PRESETS,
|
||||
int DisableCabac; // NVVE_DISABLE_CABAC,
|
||||
int NaluFramingType; // NVVE_CONFIGURE_NALU_FRAMING_TYPE
|
||||
int DisableSPSPPS; // NVVE_DISABLE_SPS_PPS
|
||||
|
||||
EncoderParams();
|
||||
explicit EncoderParams(const String& configFile);
|
||||
|
||||
void load(const String& configFile);
|
||||
void save(const String& configFile) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::EncoderParams::EncoderParams
|
||||
--------------------------------------------------
|
||||
Constructors.
|
||||
|
||||
.. ocv:function:: gpu::VideoWriter_GPU::EncoderParams::EncoderParams()
|
||||
.. ocv:function:: gpu::VideoWriter_GPU::EncoderParams::EncoderParams(const String& configFile)
|
||||
|
||||
:param configFile: Config file name.
|
||||
|
||||
Creates default parameters or reads parameters from config file.
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::EncoderParams::load
|
||||
-----------------------------------------
|
||||
Reads parameters from config file.
|
||||
|
||||
.. ocv:function:: void gpu::VideoWriter_GPU::EncoderParams::load(const String& configFile)
|
||||
|
||||
:param configFile: Config file name.
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::EncoderParams::save
|
||||
-----------------------------------------
|
||||
Saves parameters to config file.
|
||||
|
||||
.. ocv:function:: void gpu::VideoWriter_GPU::EncoderParams::save(const String& configFile) const
|
||||
|
||||
:param configFile: Config file name.
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::EncoderCallBack
|
||||
-------------------------------------
|
||||
.. ocv:class:: gpu::VideoWriter_GPU::EncoderCallBack
|
||||
|
||||
Callbacks for CUDA video encoder. ::
|
||||
|
||||
class EncoderCallBack
|
||||
{
|
||||
public:
|
||||
enum PicType
|
||||
{
|
||||
IFRAME = 1,
|
||||
PFRAME = 2,
|
||||
BFRAME = 3
|
||||
};
|
||||
|
||||
virtual ~EncoderCallBack() {}
|
||||
|
||||
virtual unsigned char* acquireBitStream(int* bufferSize) = 0;
|
||||
virtual void releaseBitStream(unsigned char* data, int size) = 0;
|
||||
virtual void onBeginFrame(int frameNumber, PicType picType) = 0;
|
||||
virtual void onEndFrame(int frameNumber, PicType picType) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::EncoderCallBack::acquireBitStream
|
||||
-------------------------------------------------------
|
||||
Callback function to signal the start of bitstream that is to be encoded.
|
||||
|
||||
.. ocv:function:: virtual uchar* gpu::VideoWriter_GPU::EncoderCallBack::acquireBitStream(int* bufferSize) = 0
|
||||
|
||||
Callback must allocate buffer for CUDA encoder and return pointer to it and it's size.
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::EncoderCallBack::releaseBitStream
|
||||
-------------------------------------------------------
|
||||
Callback function to signal that the encoded bitstream is ready to be written to file.
|
||||
|
||||
.. ocv:function:: virtual void gpu::VideoWriter_GPU::EncoderCallBack::releaseBitStream(unsigned char* data, int size) = 0
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::EncoderCallBack::onBeginFrame
|
||||
---------------------------------------------------
|
||||
Callback function to signal that the encoding operation on the frame has started.
|
||||
|
||||
.. ocv:function:: virtual void gpu::VideoWriter_GPU::EncoderCallBack::onBeginFrame(int frameNumber, PicType picType) = 0
|
||||
|
||||
:param picType: Specify frame type (I-Frame, P-Frame or B-Frame).
|
||||
|
||||
|
||||
|
||||
gpu::VideoWriter_GPU::EncoderCallBack::onEndFrame
|
||||
-------------------------------------------------
|
||||
Callback function signals that the encoding operation on the frame has finished.
|
||||
|
||||
.. ocv:function:: virtual void gpu::VideoWriter_GPU::EncoderCallBack::onEndFrame(int frameNumber, PicType picType) = 0
|
||||
|
||||
:param picType: Specify frame type (I-Frame, P-Frame or B-Frame).
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU
|
||||
--------------------
|
||||
Class for reading video from files.
|
||||
|
||||
.. ocv:class:: gpu::VideoReader_GPU
|
||||
|
||||
.. note:: Currently only Windows and Linux platforms are supported.
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::Codec
|
||||
---------------------------
|
||||
|
||||
Video codecs supported by :ocv:class:`gpu::VideoReader_GPU` .
|
||||
|
||||
.. ocv:enum:: gpu::VideoReader_GPU::Codec
|
||||
|
||||
.. ocv:emember:: MPEG1 = 0
|
||||
.. ocv:emember:: MPEG2
|
||||
.. ocv:emember:: MPEG4
|
||||
.. ocv:emember:: VC1
|
||||
.. ocv:emember:: H264
|
||||
.. ocv:emember:: JPEG
|
||||
.. ocv:emember:: H264_SVC
|
||||
.. ocv:emember:: H264_MVC
|
||||
|
||||
.. ocv:emember:: Uncompressed_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V'))
|
||||
|
||||
Y,U,V (4:2:0)
|
||||
|
||||
.. ocv:emember:: Uncompressed_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2'))
|
||||
|
||||
Y,V,U (4:2:0)
|
||||
|
||||
.. ocv:emember:: Uncompressed_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2'))
|
||||
|
||||
Y,UV (4:2:0)
|
||||
|
||||
.. ocv:emember:: Uncompressed_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V'))
|
||||
|
||||
YUYV/YUY2 (4:2:2)
|
||||
|
||||
.. ocv:emember:: Uncompressed_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y'))
|
||||
|
||||
UYVY (4:2:2)
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::ChromaFormat
|
||||
----------------------------------
|
||||
|
||||
Chroma formats supported by :ocv:class:`gpu::VideoReader_GPU` .
|
||||
|
||||
.. ocv:enum:: gpu::VideoReader_GPU::ChromaFormat
|
||||
|
||||
.. ocv:emember:: Monochrome = 0
|
||||
.. ocv:emember:: YUV420
|
||||
.. ocv:emember:: YUV422
|
||||
.. ocv:emember:: YUV444
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::FormatInfo
|
||||
--------------------------------
|
||||
.. ocv:struct:: gpu::VideoReader_GPU::FormatInfo
|
||||
|
||||
Struct providing information about video file format. ::
|
||||
|
||||
struct FormatInfo
|
||||
{
|
||||
Codec codec;
|
||||
ChromaFormat chromaFormat;
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::VideoReader_GPU
|
||||
-------------------------------------
|
||||
Constructors.
|
||||
|
||||
.. ocv:function:: gpu::VideoReader_GPU::VideoReader_GPU()
|
||||
.. ocv:function:: gpu::VideoReader_GPU::VideoReader_GPU(const String& filename)
|
||||
.. ocv:function:: gpu::VideoReader_GPU::VideoReader_GPU(const cv::Ptr<VideoSource>& source)
|
||||
|
||||
:param filename: Name of the input video file.
|
||||
|
||||
:param source: Video file parser implemented by user.
|
||||
|
||||
The constructors initialize video reader. FFMPEG is used to read videos. User can implement own demultiplexing with :ocv:class:`gpu::VideoReader_GPU::VideoSource` .
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::open
|
||||
--------------------------
|
||||
Initializes or reinitializes video reader.
|
||||
|
||||
.. ocv:function:: void gpu::VideoReader_GPU::open(const String& filename)
|
||||
.. ocv:function:: void gpu::VideoReader_GPU::open(const cv::Ptr<VideoSource>& source)
|
||||
|
||||
The method opens video reader. Parameters are the same as in the constructor :ocv:func:`gpu::VideoReader_GPU::VideoReader_GPU` . The method throws :ocv:class:`Exception` if error occurs.
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::isOpened
|
||||
------------------------------
|
||||
Returns true if video reader has been successfully initialized.
|
||||
|
||||
.. ocv:function:: bool gpu::VideoReader_GPU::isOpened() const
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::close
|
||||
---------------------------
|
||||
Releases the video reader.
|
||||
|
||||
.. ocv:function:: void gpu::VideoReader_GPU::close()
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::read
|
||||
--------------------------
|
||||
Grabs, decodes and returns the next video frame.
|
||||
|
||||
.. ocv:function:: bool gpu::VideoReader_GPU::read(GpuMat& image)
|
||||
|
||||
If no frames has been grabbed (there are no more frames in video file), the methods return ``false`` . The method throws :ocv:class:`Exception` if error occurs.
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::format
|
||||
----------------------------
|
||||
Returns information about video file format.
|
||||
|
||||
.. ocv:function:: FormatInfo gpu::VideoReader_GPU::format() const
|
||||
|
||||
The method throws :ocv:class:`Exception` if video reader wasn't initialized.
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::dumpFormat
|
||||
--------------------------------
|
||||
Dump information about video file format to specified stream.
|
||||
|
||||
.. ocv:function:: void gpu::VideoReader_GPU::dumpFormat(std::ostream& st)
|
||||
|
||||
:param st: Output stream.
|
||||
|
||||
The method throws :ocv:class:`Exception` if video reader wasn't initialized.
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::VideoSource
|
||||
-----------------------------------
|
||||
.. ocv:class:: gpu::VideoReader_GPU::VideoSource
|
||||
|
||||
Interface for video demultiplexing. ::
|
||||
|
||||
class VideoSource
|
||||
{
|
||||
public:
|
||||
VideoSource();
|
||||
virtual ~VideoSource() {}
|
||||
|
||||
virtual FormatInfo format() const = 0;
|
||||
virtual void start() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual bool isStarted() const = 0;
|
||||
virtual bool hasError() const = 0;
|
||||
|
||||
protected:
|
||||
bool parseVideoData(const unsigned char* data, size_t size, bool endOfStream = false);
|
||||
};
|
||||
|
||||
User can implement own demultiplexing by implementing this interface.
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::VideoSource::format
|
||||
-----------------------------------------
|
||||
Returns information about video file format.
|
||||
|
||||
.. ocv:function:: virtual FormatInfo gpu::VideoReader_GPU::VideoSource::format() const = 0
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::VideoSource::start
|
||||
----------------------------------------
|
||||
Starts processing.
|
||||
|
||||
.. ocv:function:: virtual void gpu::VideoReader_GPU::VideoSource::start() = 0
|
||||
|
||||
Implementation must create own thread with video processing and call periodic :ocv:func:`gpu::VideoReader_GPU::VideoSource::parseVideoData` .
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::VideoSource::stop
|
||||
---------------------------------------
|
||||
Stops processing.
|
||||
|
||||
.. ocv:function:: virtual void gpu::VideoReader_GPU::VideoSource::stop() = 0
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::VideoSource::isStarted
|
||||
--------------------------------------------
|
||||
Returns ``true`` if processing was successfully started.
|
||||
|
||||
.. ocv:function:: virtual bool gpu::VideoReader_GPU::VideoSource::isStarted() const = 0
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::VideoSource::hasError
|
||||
-------------------------------------------
|
||||
Returns ``true`` if error occured during processing.
|
||||
|
||||
.. ocv:function:: virtual bool gpu::VideoReader_GPU::VideoSource::hasError() const = 0
|
||||
|
||||
|
||||
|
||||
gpu::VideoReader_GPU::VideoSource::parseVideoData
|
||||
-------------------------------------------------
|
||||
Parse next video frame. Implementation must call this method after new frame was grabbed.
|
||||
|
||||
.. ocv:function:: bool gpu::VideoReader_GPU::VideoSource::parseVideoData(const uchar* data, size_t size, bool endOfStream = false)
|
||||
|
||||
:param data: Pointer to frame data. Can be ``NULL`` if ``endOfStream`` if ``true`` .
|
||||
|
||||
:param size: Size in bytes of current frame.
|
||||
|
||||
:param endOfStream: Indicates that it is end of stream.
|
||||
|
||||
|
||||
|
||||
.. [Brox2004] T. Brox, A. Bruhn, N. Papenberg, J. Weickert. *High accuracy optical flow estimation based on a theory for warping*. ECCV 2004.
|
||||
.. [FGD2003] Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian. *Foreground Object Detection from Videos Containing Complex Background*. ACM MM2003 9p, 2003.
|
||||
.. [MOG2001] P. KadewTraKuPong and R. Bowden. *An improved adaptive background mixture model for real-time tracking with shadow detection*. Proc. 2nd European Workshop on Advanced Video-Based Surveillance Systems, 2001
|
||||
|
@@ -2156,211 +2156,6 @@ private:
|
||||
GpuMat buf_;
|
||||
};
|
||||
|
||||
////////////////////////////////// Video Encoding //////////////////////////////////
|
||||
|
||||
// Works only under Windows
|
||||
// Supports olny H264 video codec and AVI files
|
||||
class CV_EXPORTS VideoWriter_GPU
|
||||
{
|
||||
public:
|
||||
struct EncoderParams;
|
||||
|
||||
// Callbacks for video encoder, use it if you want to work with raw video stream
|
||||
class EncoderCallBack;
|
||||
|
||||
enum SurfaceFormat
|
||||
{
|
||||
SF_UYVY = 0,
|
||||
SF_YUY2,
|
||||
SF_YV12,
|
||||
SF_NV12,
|
||||
SF_IYUV,
|
||||
SF_BGR,
|
||||
SF_GRAY = SF_BGR
|
||||
};
|
||||
|
||||
VideoWriter_GPU();
|
||||
VideoWriter_GPU(const String& fileName, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR);
|
||||
VideoWriter_GPU(const String& fileName, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
|
||||
VideoWriter_GPU(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR);
|
||||
VideoWriter_GPU(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
|
||||
~VideoWriter_GPU();
|
||||
|
||||
// all methods throws cv::Exception if error occurs
|
||||
void open(const String& fileName, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR);
|
||||
void open(const String& fileName, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
|
||||
void open(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR);
|
||||
void open(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
|
||||
|
||||
bool isOpened() const;
|
||||
void close();
|
||||
|
||||
void write(const cv::gpu::GpuMat& image, bool lastFrame = false);
|
||||
|
||||
struct CV_EXPORTS EncoderParams
|
||||
{
|
||||
int P_Interval; // NVVE_P_INTERVAL,
|
||||
int IDR_Period; // NVVE_IDR_PERIOD,
|
||||
int DynamicGOP; // NVVE_DYNAMIC_GOP,
|
||||
int RCType; // NVVE_RC_TYPE,
|
||||
int AvgBitrate; // NVVE_AVG_BITRATE,
|
||||
int PeakBitrate; // NVVE_PEAK_BITRATE,
|
||||
int QP_Level_Intra; // NVVE_QP_LEVEL_INTRA,
|
||||
int QP_Level_InterP; // NVVE_QP_LEVEL_INTER_P,
|
||||
int QP_Level_InterB; // NVVE_QP_LEVEL_INTER_B,
|
||||
int DeblockMode; // NVVE_DEBLOCK_MODE,
|
||||
int ProfileLevel; // NVVE_PROFILE_LEVEL,
|
||||
int ForceIntra; // NVVE_FORCE_INTRA,
|
||||
int ForceIDR; // NVVE_FORCE_IDR,
|
||||
int ClearStat; // NVVE_CLEAR_STAT,
|
||||
int DIMode; // NVVE_SET_DEINTERLACE,
|
||||
int Presets; // NVVE_PRESETS,
|
||||
int DisableCabac; // NVVE_DISABLE_CABAC,
|
||||
int NaluFramingType; // NVVE_CONFIGURE_NALU_FRAMING_TYPE
|
||||
int DisableSPSPPS; // NVVE_DISABLE_SPS_PPS
|
||||
|
||||
EncoderParams();
|
||||
explicit EncoderParams(const String& configFile);
|
||||
|
||||
void load(const String& configFile);
|
||||
void save(const String& configFile) const;
|
||||
};
|
||||
|
||||
EncoderParams getParams() const;
|
||||
|
||||
class CV_EXPORTS EncoderCallBack
|
||||
{
|
||||
public:
|
||||
enum PicType
|
||||
{
|
||||
IFRAME = 1,
|
||||
PFRAME = 2,
|
||||
BFRAME = 3
|
||||
};
|
||||
|
||||
virtual ~EncoderCallBack() {}
|
||||
|
||||
// callback function to signal the start of bitstream that is to be encoded
|
||||
// must return pointer to buffer
|
||||
virtual uchar* acquireBitStream(int* bufferSize) = 0;
|
||||
|
||||
// callback function to signal that the encoded bitstream is ready to be written to file
|
||||
virtual void releaseBitStream(unsigned char* data, int size) = 0;
|
||||
|
||||
// callback function to signal that the encoding operation on the frame has started
|
||||
virtual void onBeginFrame(int frameNumber, PicType picType) = 0;
|
||||
|
||||
// callback function signals that the encoding operation on the frame has finished
|
||||
virtual void onEndFrame(int frameNumber, PicType picType) = 0;
|
||||
};
|
||||
|
||||
private:
|
||||
VideoWriter_GPU(const VideoWriter_GPU&);
|
||||
VideoWriter_GPU& operator=(const VideoWriter_GPU&);
|
||||
|
||||
class Impl;
|
||||
std::auto_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////// Video Decoding //////////////////////////////////////////
|
||||
|
||||
namespace detail
|
||||
{
|
||||
class FrameQueue;
|
||||
class VideoParser;
|
||||
}
|
||||
|
||||
class CV_EXPORTS VideoReader_GPU
|
||||
{
|
||||
public:
|
||||
enum Codec
|
||||
{
|
||||
MPEG1 = 0,
|
||||
MPEG2,
|
||||
MPEG4,
|
||||
VC1,
|
||||
H264,
|
||||
JPEG,
|
||||
H264_SVC,
|
||||
H264_MVC,
|
||||
|
||||
Uncompressed_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
|
||||
Uncompressed_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,V,U (4:2:0)
|
||||
Uncompressed_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,UV (4:2:0)
|
||||
Uncompressed_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), // YUYV/YUY2 (4:2:2)
|
||||
Uncompressed_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')), // UYVY (4:2:2)
|
||||
};
|
||||
|
||||
enum ChromaFormat
|
||||
{
|
||||
Monochrome=0,
|
||||
YUV420,
|
||||
YUV422,
|
||||
YUV444,
|
||||
};
|
||||
|
||||
struct FormatInfo
|
||||
{
|
||||
Codec codec;
|
||||
ChromaFormat chromaFormat;
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
|
||||
class VideoSource;
|
||||
|
||||
VideoReader_GPU();
|
||||
explicit VideoReader_GPU(const String& filename);
|
||||
explicit VideoReader_GPU(const cv::Ptr<VideoSource>& source);
|
||||
|
||||
~VideoReader_GPU();
|
||||
|
||||
void open(const String& filename);
|
||||
void open(const cv::Ptr<VideoSource>& source);
|
||||
bool isOpened() const;
|
||||
|
||||
void close();
|
||||
|
||||
bool read(GpuMat& image);
|
||||
|
||||
FormatInfo format() const;
|
||||
void dumpFormat(std::ostream& st);
|
||||
|
||||
class CV_EXPORTS VideoSource
|
||||
{
|
||||
public:
|
||||
VideoSource() : frameQueue_(0), videoParser_(0) {}
|
||||
virtual ~VideoSource() {}
|
||||
|
||||
virtual FormatInfo format() const = 0;
|
||||
virtual void start() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual bool isStarted() const = 0;
|
||||
virtual bool hasError() const = 0;
|
||||
|
||||
void setFrameQueue(detail::FrameQueue* frameQueue) { frameQueue_ = frameQueue; }
|
||||
void setVideoParser(detail::VideoParser* videoParser) { videoParser_ = videoParser; }
|
||||
|
||||
protected:
|
||||
bool parseVideoData(const uchar* data, size_t size, bool endOfStream = false);
|
||||
|
||||
private:
|
||||
VideoSource(const VideoSource&);
|
||||
VideoSource& operator =(const VideoSource&);
|
||||
|
||||
detail::FrameQueue* frameQueue_;
|
||||
detail::VideoParser* videoParser_;
|
||||
};
|
||||
|
||||
private:
|
||||
VideoReader_GPU(const VideoReader_GPU&);
|
||||
VideoReader_GPU& operator =(const VideoReader_GPU&);
|
||||
|
||||
class Impl;
|
||||
std::auto_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
//! removes points (CV_32FC2, single row matrix) with zero mask value
|
||||
CV_EXPORTS void compactPoints(GpuMat &points0, GpuMat &points1, const GpuMat &mask);
|
||||
|
||||
|
@@ -1005,103 +1005,3 @@ PERF_TEST_P(Video_Cn_MaxFeatures, Video_GMG,
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// VideoReader
|
||||
|
||||
#if defined(HAVE_NVCUVID) && BUILD_WITH_VIDEO_INPUT_SUPPORT
|
||||
|
||||
PERF_TEST_P(Video, DISABLED_Video_VideoReader, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))
|
||||
{
|
||||
declare.time(20);
|
||||
|
||||
const string inputFile = perf::TestBase::getDataPath(GetParam());
|
||||
|
||||
if (PERF_RUN_GPU())
|
||||
{
|
||||
cv::gpu::VideoReader_GPU d_reader(inputFile);
|
||||
ASSERT_TRUE( d_reader.isOpened() );
|
||||
|
||||
cv::gpu::GpuMat frame;
|
||||
|
||||
TEST_CYCLE_N(10) d_reader.read(frame);
|
||||
|
||||
GPU_SANITY_CHECK(frame);
|
||||
}
|
||||
else
|
||||
{
|
||||
cv::VideoCapture reader(inputFile);
|
||||
ASSERT_TRUE( reader.isOpened() );
|
||||
|
||||
cv::Mat frame;
|
||||
|
||||
TEST_CYCLE_N(10) reader >> frame;
|
||||
|
||||
CPU_SANITY_CHECK(frame);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// VideoWriter
|
||||
|
||||
#if defined(HAVE_NVCUVID) && defined(WIN32)
|
||||
|
||||
PERF_TEST_P(Video, DISABLED_Video_VideoWriter, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))
|
||||
{
|
||||
declare.time(30);
|
||||
|
||||
const string inputFile = perf::TestBase::getDataPath(GetParam());
|
||||
const string outputFile = cv::tempfile(".avi");
|
||||
|
||||
const double FPS = 25.0;
|
||||
|
||||
cv::VideoCapture reader(inputFile);
|
||||
ASSERT_TRUE( reader.isOpened() );
|
||||
|
||||
cv::Mat frame;
|
||||
|
||||
if (PERF_RUN_GPU())
|
||||
{
|
||||
cv::gpu::VideoWriter_GPU d_writer;
|
||||
|
||||
cv::gpu::GpuMat d_frame;
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
reader >> frame;
|
||||
ASSERT_FALSE(frame.empty());
|
||||
|
||||
d_frame.upload(frame);
|
||||
|
||||
if (!d_writer.isOpened())
|
||||
d_writer.open(outputFile, frame.size(), FPS);
|
||||
|
||||
startTimer(); next();
|
||||
d_writer.write(d_frame);
|
||||
stopTimer();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cv::VideoWriter writer;
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
reader >> frame;
|
||||
ASSERT_FALSE(frame.empty());
|
||||
|
||||
if (!writer.isOpened())
|
||||
writer.open(outputFile, CV_FOURCC('X', 'V', 'I', 'D'), FPS, frame.size());
|
||||
|
||||
startTimer(); next();
|
||||
writer.write(frame);
|
||||
stopTimer();
|
||||
}
|
||||
}
|
||||
|
||||
SANITY_CHECK(frame);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -1,201 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
/*
|
||||
* NV12ToARGB color space conversion CUDA kernel
|
||||
*
|
||||
* This sample uses CUDA to perform a simple NV12 (YUV 4:2:0 planar)
|
||||
* source and converts to output in ARGB format
|
||||
*/
|
||||
|
||||
#if !defined CUDA_DISABLER
|
||||
|
||||
#include "opencv2/core/cuda/common.hpp"
|
||||
|
||||
namespace cv { namespace gpu { namespace cudev {
|
||||
namespace video_decoding
|
||||
{
|
||||
__constant__ uint constAlpha = ((uint)0xff << 24);
|
||||
|
||||
__constant__ float constHueColorSpaceMat[9];
|
||||
|
||||
void loadHueCSC(float hueCSC[9])
|
||||
{
|
||||
cudaSafeCall( cudaMemcpyToSymbol(constHueColorSpaceMat, hueCSC, 9 * sizeof(float)) );
|
||||
}
|
||||
|
||||
__device__ void YUV2RGB(const uint* yuvi, float* red, float* green, float* blue)
|
||||
{
|
||||
float luma, chromaCb, chromaCr;
|
||||
|
||||
// Prepare for hue adjustment
|
||||
luma = (float)yuvi[0];
|
||||
chromaCb = (float)((int)yuvi[1] - 512.0f);
|
||||
chromaCr = (float)((int)yuvi[2] - 512.0f);
|
||||
|
||||
// Convert YUV To RGB with hue adjustment
|
||||
*red = (luma * constHueColorSpaceMat[0]) +
|
||||
(chromaCb * constHueColorSpaceMat[1]) +
|
||||
(chromaCr * constHueColorSpaceMat[2]);
|
||||
|
||||
*green = (luma * constHueColorSpaceMat[3]) +
|
||||
(chromaCb * constHueColorSpaceMat[4]) +
|
||||
(chromaCr * constHueColorSpaceMat[5]);
|
||||
|
||||
*blue = (luma * constHueColorSpaceMat[6]) +
|
||||
(chromaCb * constHueColorSpaceMat[7]) +
|
||||
(chromaCr * constHueColorSpaceMat[8]);
|
||||
}
|
||||
|
||||
__device__ uint RGBAPACK_10bit(float red, float green, float blue, uint alpha)
|
||||
{
|
||||
uint ARGBpixel = 0;
|
||||
|
||||
// Clamp final 10 bit results
|
||||
red = ::fmin(::fmax(red, 0.0f), 1023.f);
|
||||
green = ::fmin(::fmax(green, 0.0f), 1023.f);
|
||||
blue = ::fmin(::fmax(blue, 0.0f), 1023.f);
|
||||
|
||||
// Convert to 8 bit unsigned integers per color component
|
||||
ARGBpixel = (((uint)blue >> 2) |
|
||||
(((uint)green >> 2) << 8) |
|
||||
(((uint)red >> 2) << 16) |
|
||||
(uint)alpha);
|
||||
|
||||
return ARGBpixel;
|
||||
}
|
||||
|
||||
// CUDA kernel for outputing the final ARGB output from NV12
|
||||
|
||||
#define COLOR_COMPONENT_BIT_SIZE 10
|
||||
#define COLOR_COMPONENT_MASK 0x3FF
|
||||
|
||||
__global__ void NV12ToARGB(uchar* srcImage, size_t nSourcePitch,
|
||||
uint* dstImage, size_t nDestPitch,
|
||||
uint width, uint height)
|
||||
{
|
||||
// Pad borders with duplicate pixels, and we multiply by 2 because we process 2 pixels per thread
|
||||
const int x = blockIdx.x * (blockDim.x << 1) + (threadIdx.x << 1);
|
||||
const int y = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
|
||||
if (x >= width || y >= height)
|
||||
return;
|
||||
|
||||
// Read 2 Luma components at a time, so we don't waste processing since CbCr are decimated this way.
|
||||
// if we move to texture we could read 4 luminance values
|
||||
|
||||
uint yuv101010Pel[2];
|
||||
|
||||
yuv101010Pel[0] = (srcImage[y * nSourcePitch + x ]) << 2;
|
||||
yuv101010Pel[1] = (srcImage[y * nSourcePitch + x + 1]) << 2;
|
||||
|
||||
const size_t chromaOffset = nSourcePitch * height;
|
||||
|
||||
const int y_chroma = y >> 1;
|
||||
|
||||
if (y & 1) // odd scanline ?
|
||||
{
|
||||
uint chromaCb = srcImage[chromaOffset + y_chroma * nSourcePitch + x ];
|
||||
uint chromaCr = srcImage[chromaOffset + y_chroma * nSourcePitch + x + 1];
|
||||
|
||||
if (y_chroma < ((height >> 1) - 1)) // interpolate chroma vertically
|
||||
{
|
||||
chromaCb = (chromaCb + srcImage[chromaOffset + (y_chroma + 1) * nSourcePitch + x ] + 1) >> 1;
|
||||
chromaCr = (chromaCr + srcImage[chromaOffset + (y_chroma + 1) * nSourcePitch + x + 1] + 1) >> 1;
|
||||
}
|
||||
|
||||
yuv101010Pel[0] |= (chromaCb << ( COLOR_COMPONENT_BIT_SIZE + 2));
|
||||
yuv101010Pel[0] |= (chromaCr << ((COLOR_COMPONENT_BIT_SIZE << 1) + 2));
|
||||
|
||||
yuv101010Pel[1] |= (chromaCb << ( COLOR_COMPONENT_BIT_SIZE + 2));
|
||||
yuv101010Pel[1] |= (chromaCr << ((COLOR_COMPONENT_BIT_SIZE << 1) + 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
yuv101010Pel[0] |= ((uint)srcImage[chromaOffset + y_chroma * nSourcePitch + x ] << ( COLOR_COMPONENT_BIT_SIZE + 2));
|
||||
yuv101010Pel[0] |= ((uint)srcImage[chromaOffset + y_chroma * nSourcePitch + x + 1] << ((COLOR_COMPONENT_BIT_SIZE << 1) + 2));
|
||||
|
||||
yuv101010Pel[1] |= ((uint)srcImage[chromaOffset + y_chroma * nSourcePitch + x ] << ( COLOR_COMPONENT_BIT_SIZE + 2));
|
||||
yuv101010Pel[1] |= ((uint)srcImage[chromaOffset + y_chroma * nSourcePitch + x + 1] << ((COLOR_COMPONENT_BIT_SIZE << 1) + 2));
|
||||
}
|
||||
|
||||
// this steps performs the color conversion
|
||||
uint yuvi[6];
|
||||
float red[2], green[2], blue[2];
|
||||
|
||||
yuvi[0] = (yuv101010Pel[0] & COLOR_COMPONENT_MASK );
|
||||
yuvi[1] = ((yuv101010Pel[0] >> COLOR_COMPONENT_BIT_SIZE) & COLOR_COMPONENT_MASK);
|
||||
yuvi[2] = ((yuv101010Pel[0] >> (COLOR_COMPONENT_BIT_SIZE << 1)) & COLOR_COMPONENT_MASK);
|
||||
|
||||
yuvi[3] = (yuv101010Pel[1] & COLOR_COMPONENT_MASK );
|
||||
yuvi[4] = ((yuv101010Pel[1] >> COLOR_COMPONENT_BIT_SIZE) & COLOR_COMPONENT_MASK);
|
||||
yuvi[5] = ((yuv101010Pel[1] >> (COLOR_COMPONENT_BIT_SIZE << 1)) & COLOR_COMPONENT_MASK);
|
||||
|
||||
// YUV to RGB Transformation conversion
|
||||
YUV2RGB(&yuvi[0], &red[0], &green[0], &blue[0]);
|
||||
YUV2RGB(&yuvi[3], &red[1], &green[1], &blue[1]);
|
||||
|
||||
// Clamp the results to RGBA
|
||||
|
||||
const size_t dstImagePitch = nDestPitch >> 2;
|
||||
|
||||
dstImage[y * dstImagePitch + x ] = RGBAPACK_10bit(red[0], green[0], blue[0], constAlpha);
|
||||
dstImage[y * dstImagePitch + x + 1 ] = RGBAPACK_10bit(red[1], green[1], blue[1], constAlpha);
|
||||
}
|
||||
|
||||
void NV12ToARGB_gpu(const PtrStepb decodedFrame, PtrStepSz<uint> interopFrame, cudaStream_t stream)
|
||||
{
|
||||
dim3 block(32, 8);
|
||||
dim3 grid(divUp(interopFrame.cols, 2 * block.x), divUp(interopFrame.rows, block.y));
|
||||
|
||||
NV12ToARGB<<<grid, block, 0, stream>>>(decodedFrame.data, decodedFrame.step, interopFrame.data, interopFrame.step,
|
||||
interopFrame.cols, interopFrame.rows);
|
||||
|
||||
cudaSafeCall( cudaGetLastError() );
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
}
|
||||
}}}
|
||||
|
||||
#endif /* CUDA_DISABLER */
|
@@ -1,175 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#if !defined CUDA_DISABLER
|
||||
|
||||
#include "opencv2/core/cuda/common.hpp"
|
||||
#include "opencv2/core/cuda/vec_traits.hpp"
|
||||
|
||||
namespace cv { namespace gpu { namespace cudev
|
||||
{
|
||||
namespace video_encoding
|
||||
{
|
||||
__device__ __forceinline__ void rgbtoy(const uchar b, const uchar g, const uchar r, uchar& y)
|
||||
{
|
||||
y = static_cast<uchar>(((int)(30 * r) + (int)(59 * g) + (int)(11 * b)) / 100);
|
||||
}
|
||||
|
||||
__device__ __forceinline__ void rgbtoyuv(const uchar b, const uchar g, const uchar r, uchar& y, uchar& u, uchar& v)
|
||||
{
|
||||
rgbtoy(b, g, r, y);
|
||||
u = static_cast<uchar>(((int)(-17 * r) - (int)(33 * g) + (int)(50 * b) + 12800) / 100);
|
||||
v = static_cast<uchar>(((int)(50 * r) - (int)(42 * g) - (int)(8 * b) + 12800) / 100);
|
||||
}
|
||||
|
||||
__global__ void Gray_to_YV12(const PtrStepSzb src, PtrStepb dst)
|
||||
{
|
||||
const int x = (blockIdx.x * blockDim.x + threadIdx.x) * 2;
|
||||
const int y = (blockIdx.y * blockDim.y + threadIdx.y) * 2;
|
||||
|
||||
if (x + 1 >= src.cols || y + 1 >= src.rows)
|
||||
return;
|
||||
|
||||
// get pointers to the data
|
||||
const size_t planeSize = src.rows * dst.step;
|
||||
PtrStepb y_plane(dst.data, dst.step);
|
||||
PtrStepb u_plane(y_plane.data + planeSize, dst.step / 2);
|
||||
PtrStepb v_plane(u_plane.data + (planeSize / 4), dst.step / 2);
|
||||
|
||||
uchar pix;
|
||||
uchar y_val, u_val, v_val;
|
||||
|
||||
pix = src(y, x);
|
||||
rgbtoy(pix, pix, pix, y_val);
|
||||
y_plane(y, x) = y_val;
|
||||
|
||||
pix = src(y, x + 1);
|
||||
rgbtoy(pix, pix, pix, y_val);
|
||||
y_plane(y, x + 1) = y_val;
|
||||
|
||||
pix = src(y + 1, x);
|
||||
rgbtoy(pix, pix, pix, y_val);
|
||||
y_plane(y + 1, x) = y_val;
|
||||
|
||||
pix = src(y + 1, x + 1);
|
||||
rgbtoyuv(pix, pix, pix, y_val, u_val, v_val);
|
||||
y_plane(y + 1, x + 1) = y_val;
|
||||
u_plane(y / 2, x / 2) = u_val;
|
||||
v_plane(y / 2, x / 2) = v_val;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
__global__ void BGR_to_YV12(const PtrStepSz<T> src, PtrStepb dst)
|
||||
{
|
||||
const int x = (blockIdx.x * blockDim.x + threadIdx.x) * 2;
|
||||
const int y = (blockIdx.y * blockDim.y + threadIdx.y) * 2;
|
||||
|
||||
if (x + 1 >= src.cols || y + 1 >= src.rows)
|
||||
return;
|
||||
|
||||
// get pointers to the data
|
||||
const size_t planeSize = src.rows * dst.step;
|
||||
PtrStepb y_plane(dst.data, dst.step);
|
||||
PtrStepb u_plane(y_plane.data + planeSize, dst.step / 2);
|
||||
PtrStepb v_plane(u_plane.data + (planeSize / 4), dst.step / 2);
|
||||
|
||||
T pix;
|
||||
uchar y_val, u_val, v_val;
|
||||
|
||||
pix = src(y, x);
|
||||
rgbtoy(pix.z, pix.y, pix.x, y_val);
|
||||
y_plane(y, x) = y_val;
|
||||
|
||||
pix = src(y, x + 1);
|
||||
rgbtoy(pix.z, pix.y, pix.x, y_val);
|
||||
y_plane(y, x + 1) = y_val;
|
||||
|
||||
pix = src(y + 1, x);
|
||||
rgbtoy(pix.z, pix.y, pix.x, y_val);
|
||||
y_plane(y + 1, x) = y_val;
|
||||
|
||||
pix = src(y + 1, x + 1);
|
||||
rgbtoyuv(pix.z, pix.y, pix.x, y_val, u_val, v_val);
|
||||
y_plane(y + 1, x + 1) = y_val;
|
||||
u_plane(y / 2, x / 2) = u_val;
|
||||
v_plane(y / 2, x / 2) = v_val;
|
||||
}
|
||||
|
||||
void Gray_to_YV12_caller(const PtrStepSzb src, PtrStepb dst)
|
||||
{
|
||||
dim3 block(32, 8);
|
||||
dim3 grid(divUp(src.cols, block.x * 2), divUp(src.rows, block.y * 2));
|
||||
|
||||
Gray_to_YV12<<<grid, block>>>(src, dst);
|
||||
cudaSafeCall( cudaGetLastError() );
|
||||
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
template <int cn>
|
||||
void BGR_to_YV12_caller(const PtrStepSzb src, PtrStepb dst)
|
||||
{
|
||||
typedef typename TypeVec<uchar, cn>::vec_type src_t;
|
||||
|
||||
dim3 block(32, 8);
|
||||
dim3 grid(divUp(src.cols, block.x * 2), divUp(src.rows, block.y * 2));
|
||||
|
||||
BGR_to_YV12<<<grid, block>>>(static_cast< PtrStepSz<src_t> >(src), dst);
|
||||
cudaSafeCall( cudaGetLastError() );
|
||||
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
|
||||
void YV12_gpu(const PtrStepSzb src, int cn, PtrStepSzb dst)
|
||||
{
|
||||
typedef void (*func_t)(const PtrStepSzb src, PtrStepb dst);
|
||||
|
||||
static const func_t funcs[] =
|
||||
{
|
||||
0, Gray_to_YV12_caller, 0, BGR_to_YV12_caller<3>, BGR_to_YV12_caller<4>
|
||||
};
|
||||
|
||||
funcs[cn](src, dst);
|
||||
}
|
||||
}
|
||||
}}}
|
||||
|
||||
#endif /* CUDA_DISABLER */
|
@@ -1,104 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "cuvid_video_source.h"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
cv::gpu::detail::CuvidVideoSource::CuvidVideoSource(const String& fname)
|
||||
{
|
||||
CUVIDSOURCEPARAMS params;
|
||||
std::memset(¶ms, 0, sizeof(CUVIDSOURCEPARAMS));
|
||||
|
||||
// Fill parameter struct
|
||||
params.pUserData = this; // will be passed to data handlers
|
||||
params.pfnVideoDataHandler = HandleVideoData; // our local video-handler callback
|
||||
params.pfnAudioDataHandler = 0;
|
||||
|
||||
// now create the actual source
|
||||
CUresult res = cuvidCreateVideoSource(&videoSource_, fname.c_str(), ¶ms);
|
||||
if (res == CUDA_ERROR_INVALID_SOURCE)
|
||||
throw std::runtime_error("Unsupported video source");
|
||||
cuSafeCall( res );
|
||||
|
||||
CUVIDEOFORMAT vidfmt;
|
||||
cuSafeCall( cuvidGetSourceVideoFormat(videoSource_, &vidfmt, 0) );
|
||||
|
||||
format_.codec = static_cast<VideoReader_GPU::Codec>(vidfmt.codec);
|
||||
format_.chromaFormat = static_cast<VideoReader_GPU::ChromaFormat>(vidfmt.chroma_format);
|
||||
format_.width = vidfmt.coded_width;
|
||||
format_.height = vidfmt.coded_height;
|
||||
}
|
||||
|
||||
cv::gpu::VideoReader_GPU::FormatInfo cv::gpu::detail::CuvidVideoSource::format() const
|
||||
{
|
||||
return format_;
|
||||
}
|
||||
|
||||
void cv::gpu::detail::CuvidVideoSource::start()
|
||||
{
|
||||
cuSafeCall( cuvidSetVideoSourceState(videoSource_, cudaVideoState_Started) );
|
||||
}
|
||||
|
||||
void cv::gpu::detail::CuvidVideoSource::stop()
|
||||
{
|
||||
cuSafeCall( cuvidSetVideoSourceState(videoSource_, cudaVideoState_Stopped) );
|
||||
}
|
||||
|
||||
bool cv::gpu::detail::CuvidVideoSource::isStarted() const
|
||||
{
|
||||
return (cuvidGetVideoSourceState(videoSource_) == cudaVideoState_Started);
|
||||
}
|
||||
|
||||
bool cv::gpu::detail::CuvidVideoSource::hasError() const
|
||||
{
|
||||
return (cuvidGetVideoSourceState(videoSource_) == cudaVideoState_Error);
|
||||
}
|
||||
|
||||
int CUDAAPI cv::gpu::detail::CuvidVideoSource::HandleVideoData(void* userData, CUVIDSOURCEDATAPACKET* packet)
|
||||
{
|
||||
CuvidVideoSource* thiz = static_cast<CuvidVideoSource*>(userData);
|
||||
|
||||
return thiz->parseVideoData(packet->payload, packet->payload_size, (packet->flags & CUVID_PKT_ENDOFSTREAM) != 0);
|
||||
}
|
||||
|
||||
#endif // defined(HAVE_CUDA) && !defined(__APPLE__)
|
@@ -1,90 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#ifndef __CUVUD_VIDEO_SOURCE_H__
|
||||
#define __CUVUD_VIDEO_SOURCE_H__
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
namespace cv { namespace gpu
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
class CuvidVideoSource : public VideoReader_GPU::VideoSource
|
||||
{
|
||||
public:
|
||||
explicit CuvidVideoSource(const String& fname);
|
||||
~CuvidVideoSource() { cuvidDestroyVideoSource(videoSource_); }
|
||||
|
||||
VideoReader_GPU::FormatInfo format() const;
|
||||
void start();
|
||||
void stop();
|
||||
bool isStarted() const;
|
||||
bool hasError() const;
|
||||
|
||||
private:
|
||||
CuvidVideoSource(const CuvidVideoSource&);
|
||||
CuvidVideoSource& operator =(const CuvidVideoSource&);
|
||||
|
||||
// Callback for handling packages of demuxed video data.
|
||||
//
|
||||
// Parameters:
|
||||
// pUserData - Pointer to user data. We must pass a pointer to a
|
||||
// VideoSourceData struct here, that contains a valid CUvideoparser
|
||||
// and FrameQueue.
|
||||
// pPacket - video-source data packet.
|
||||
//
|
||||
// NOTE: called from a different thread that doesn't not have a cuda context
|
||||
//
|
||||
static int CUDAAPI HandleVideoData(void* pUserData, CUVIDSOURCEDATAPACKET* pPacket);
|
||||
|
||||
CUvideosource videoSource_;
|
||||
VideoReader_GPU::FormatInfo format_;
|
||||
};
|
||||
}
|
||||
}}
|
||||
|
||||
#endif // defined(HAVE_CUDA) && !defined(__APPLE__)
|
||||
|
||||
#endif // __CUVUD_VIDEO_SOURCE_H__
|
@@ -1,182 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "ffmpeg_video_source.h"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
#if defined(HAVE_FFMPEG) && defined(BUILD_SHARED_LIBS)
|
||||
#include "../src/cap_ffmpeg_impl.hpp"
|
||||
#else
|
||||
#include "../src/cap_ffmpeg_api.hpp"
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
Create_InputMediaStream_FFMPEG_Plugin create_InputMediaStream_FFMPEG_p = 0;
|
||||
Release_InputMediaStream_FFMPEG_Plugin release_InputMediaStream_FFMPEG_p = 0;
|
||||
Read_InputMediaStream_FFMPEG_Plugin read_InputMediaStream_FFMPEG_p = 0;
|
||||
|
||||
bool init_MediaStream_FFMPEG()
|
||||
{
|
||||
static bool initialized = 0;
|
||||
|
||||
if (!initialized)
|
||||
{
|
||||
#if defined WIN32 || defined _WIN32
|
||||
const char* module_name = "opencv_ffmpeg"
|
||||
CVAUX_STR(CV_VERSION_EPOCH) CVAUX_STR(CV_VERSION_MAJOR) CVAUX_STR(CV_VERSION_MINOR)
|
||||
#if (defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__)
|
||||
"_64"
|
||||
#endif
|
||||
".dll";
|
||||
|
||||
static HMODULE cvFFOpenCV = LoadLibrary(module_name);
|
||||
|
||||
if (cvFFOpenCV)
|
||||
{
|
||||
create_InputMediaStream_FFMPEG_p =
|
||||
(Create_InputMediaStream_FFMPEG_Plugin)GetProcAddress(cvFFOpenCV, "create_InputMediaStream_FFMPEG");
|
||||
release_InputMediaStream_FFMPEG_p =
|
||||
(Release_InputMediaStream_FFMPEG_Plugin)GetProcAddress(cvFFOpenCV, "release_InputMediaStream_FFMPEG");
|
||||
read_InputMediaStream_FFMPEG_p =
|
||||
(Read_InputMediaStream_FFMPEG_Plugin)GetProcAddress(cvFFOpenCV, "read_InputMediaStream_FFMPEG");
|
||||
|
||||
initialized = create_InputMediaStream_FFMPEG_p != 0 && release_InputMediaStream_FFMPEG_p != 0 && read_InputMediaStream_FFMPEG_p != 0;
|
||||
}
|
||||
#elif defined HAVE_FFMPEG
|
||||
create_InputMediaStream_FFMPEG_p = create_InputMediaStream_FFMPEG;
|
||||
release_InputMediaStream_FFMPEG_p = release_InputMediaStream_FFMPEG;
|
||||
read_InputMediaStream_FFMPEG_p = read_InputMediaStream_FFMPEG;
|
||||
|
||||
initialized = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
return initialized;
|
||||
}
|
||||
}
|
||||
|
||||
cv::gpu::detail::FFmpegVideoSource::FFmpegVideoSource(const String& fname) :
|
||||
stream_(0)
|
||||
{
|
||||
CV_Assert( init_MediaStream_FFMPEG() );
|
||||
|
||||
int codec;
|
||||
int chroma_format;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
stream_ = create_InputMediaStream_FFMPEG_p(fname.c_str(), &codec, &chroma_format, &width, &height);
|
||||
if (!stream_)
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported video source");
|
||||
|
||||
format_.codec = static_cast<VideoReader_GPU::Codec>(codec);
|
||||
format_.chromaFormat = static_cast<VideoReader_GPU::ChromaFormat>(chroma_format);
|
||||
format_.width = width;
|
||||
format_.height = height;
|
||||
}
|
||||
|
||||
cv::gpu::detail::FFmpegVideoSource::~FFmpegVideoSource()
|
||||
{
|
||||
release_InputMediaStream_FFMPEG_p(stream_);
|
||||
}
|
||||
|
||||
cv::gpu::VideoReader_GPU::FormatInfo cv::gpu::detail::FFmpegVideoSource::format() const
|
||||
{
|
||||
return format_;
|
||||
}
|
||||
|
||||
void cv::gpu::detail::FFmpegVideoSource::start()
|
||||
{
|
||||
stop_ = false;
|
||||
hasError_ = false;
|
||||
thread_.reset(new Thread(readLoop, this));
|
||||
}
|
||||
|
||||
void cv::gpu::detail::FFmpegVideoSource::stop()
|
||||
{
|
||||
stop_ = true;
|
||||
thread_->wait();
|
||||
thread_.reset();
|
||||
}
|
||||
|
||||
bool cv::gpu::detail::FFmpegVideoSource::isStarted() const
|
||||
{
|
||||
return !stop_;
|
||||
}
|
||||
|
||||
bool cv::gpu::detail::FFmpegVideoSource::hasError() const
|
||||
{
|
||||
return hasError_;
|
||||
}
|
||||
|
||||
void cv::gpu::detail::FFmpegVideoSource::readLoop(void* userData)
|
||||
{
|
||||
FFmpegVideoSource* thiz = static_cast<FFmpegVideoSource*>(userData);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
unsigned char* data;
|
||||
int size;
|
||||
int endOfFile;
|
||||
|
||||
if (!read_InputMediaStream_FFMPEG_p(thiz->stream_, &data, &size, &endOfFile))
|
||||
{
|
||||
thiz->hasError_ = !endOfFile;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!thiz->parseVideoData(data, size))
|
||||
{
|
||||
thiz->hasError_ = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (thiz->stop_)
|
||||
break;
|
||||
}
|
||||
|
||||
thiz->parseVideoData(0, 0, true);
|
||||
}
|
||||
|
||||
#endif // HAVE_CUDA
|
@@ -1,88 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#ifndef __FFMPEG_VIDEO_SOURCE_H__
|
||||
#define __FFMPEG_VIDEO_SOURCE_H__
|
||||
|
||||
#include "precomp.hpp"
|
||||
#include "thread_wrappers.h"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
struct InputMediaStream_FFMPEG;
|
||||
|
||||
namespace cv { namespace gpu
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
class FFmpegVideoSource : public VideoReader_GPU::VideoSource
|
||||
{
|
||||
public:
|
||||
FFmpegVideoSource(const String& fname);
|
||||
~FFmpegVideoSource();
|
||||
|
||||
VideoReader_GPU::FormatInfo format() const;
|
||||
void start();
|
||||
void stop();
|
||||
bool isStarted() const;
|
||||
bool hasError() const;
|
||||
|
||||
private:
|
||||
FFmpegVideoSource(const FFmpegVideoSource&);
|
||||
FFmpegVideoSource& operator =(const FFmpegVideoSource&);
|
||||
|
||||
VideoReader_GPU::FormatInfo format_;
|
||||
|
||||
InputMediaStream_FFMPEG* stream_;
|
||||
|
||||
std::auto_ptr<Thread> thread_;
|
||||
volatile bool stop_;
|
||||
volatile bool hasError_;
|
||||
|
||||
static void readLoop(void* userData);
|
||||
};
|
||||
}
|
||||
}}
|
||||
|
||||
#endif // HAVE_CUDA
|
||||
|
||||
#endif // __CUVUD_VIDEO_SOURCE_H__
|
@@ -1,117 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "frame_queue.h"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
cv::gpu::detail::FrameQueue::FrameQueue() :
|
||||
endOfDecode_(0),
|
||||
framesInQueue_(0),
|
||||
readPosition_(0)
|
||||
{
|
||||
std::memset(displayQueue_, 0, sizeof(displayQueue_));
|
||||
std::memset((void*)isFrameInUse_, 0, sizeof(isFrameInUse_));
|
||||
}
|
||||
|
||||
bool cv::gpu::detail::FrameQueue::waitUntilFrameAvailable(int pictureIndex)
|
||||
{
|
||||
while (isInUse(pictureIndex))
|
||||
{
|
||||
// Decoder is getting too far ahead from display
|
||||
Thread::sleep(1);
|
||||
|
||||
if (isEndOfDecode())
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void cv::gpu::detail::FrameQueue::enqueue(const CUVIDPARSERDISPINFO* picParams)
|
||||
{
|
||||
// Mark the frame as 'in-use' so we don't re-use it for decoding until it is no longer needed
|
||||
// for display
|
||||
isFrameInUse_[picParams->picture_index] = true;
|
||||
|
||||
// Wait until we have a free entry in the display queue (should never block if we have enough entries)
|
||||
do
|
||||
{
|
||||
bool isFramePlaced = false;
|
||||
|
||||
{
|
||||
CriticalSection::AutoLock autoLock(criticalSection_);
|
||||
|
||||
if (framesInQueue_ < MaximumSize)
|
||||
{
|
||||
int writePosition = (readPosition_ + framesInQueue_) % MaximumSize;
|
||||
displayQueue_[writePosition] = *picParams;
|
||||
framesInQueue_++;
|
||||
isFramePlaced = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isFramePlaced) // Done
|
||||
break;
|
||||
|
||||
// Wait a bit
|
||||
Thread::sleep(1);
|
||||
} while (!isEndOfDecode());
|
||||
}
|
||||
|
||||
bool cv::gpu::detail::FrameQueue::dequeue(CUVIDPARSERDISPINFO& displayInfo)
|
||||
{
|
||||
CriticalSection::AutoLock autoLock(criticalSection_);
|
||||
|
||||
if (framesInQueue_ > 0)
|
||||
{
|
||||
int entry = readPosition_;
|
||||
displayInfo = displayQueue_[entry];
|
||||
readPosition_ = (entry + 1) % MaximumSize;
|
||||
framesInQueue_--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif // HAVE_CUDA
|
@@ -1,103 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#ifndef __FRAME_QUEUE_H__
|
||||
#define __FRAME_QUEUE_H__
|
||||
|
||||
#include "precomp.hpp"
|
||||
#include "thread_wrappers.h"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
namespace cv { namespace gpu
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
class FrameQueue
|
||||
{
|
||||
public:
|
||||
static const int MaximumSize = 20; // MAX_FRM_CNT;
|
||||
|
||||
FrameQueue();
|
||||
|
||||
void endDecode() { endOfDecode_ = true; }
|
||||
bool isEndOfDecode() const { return endOfDecode_ != 0;}
|
||||
|
||||
// Spins until frame becomes available or decoding gets canceled.
|
||||
// If the requested frame is available the method returns true.
|
||||
// If decoding was interupted before the requested frame becomes
|
||||
// available, the method returns false.
|
||||
bool waitUntilFrameAvailable(int pictureIndex);
|
||||
|
||||
void enqueue(const CUVIDPARSERDISPINFO* picParams);
|
||||
|
||||
// Deque the next frame.
|
||||
// Parameters:
|
||||
// displayInfo - New frame info gets placed into this object.
|
||||
// Returns:
|
||||
// true, if a new frame was returned,
|
||||
// false, if the queue was empty and no new frame could be returned.
|
||||
bool dequeue(CUVIDPARSERDISPINFO& displayInfo);
|
||||
|
||||
void releaseFrame(const CUVIDPARSERDISPINFO& picParams) { isFrameInUse_[picParams.picture_index] = false; }
|
||||
|
||||
private:
|
||||
FrameQueue(const FrameQueue&);
|
||||
FrameQueue& operator =(const FrameQueue&);
|
||||
|
||||
bool isInUse(int pictureIndex) const { return isFrameInUse_[pictureIndex] != 0; }
|
||||
|
||||
CriticalSection criticalSection_;
|
||||
|
||||
volatile int isFrameInUse_[MaximumSize];
|
||||
volatile int endOfDecode_;
|
||||
|
||||
int framesInQueue_;
|
||||
int readPosition_;
|
||||
CUVIDPARSERDISPINFO displayQueue_[MaximumSize];
|
||||
};
|
||||
}
|
||||
}}
|
||||
|
||||
#endif // HAVE_CUDA
|
||||
|
||||
#endif // __FRAME_QUEUE_H__
|
@@ -80,20 +80,6 @@
|
||||
#include <cublas.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_NVCUVID
|
||||
#include <nvcuvid.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#undef small
|
||||
#undef min
|
||||
#undef max
|
||||
#undef abs
|
||||
|
||||
#include <NVEncoderAPI.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "internal_shared.hpp"
|
||||
#include "opencv2/core/stream_accessor.hpp"
|
||||
|
||||
|
@@ -1,254 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "thread_wrappers.h"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
#ifdef WIN32
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
class cv::gpu::detail::CriticalSection::Impl
|
||||
{
|
||||
public:
|
||||
Impl()
|
||||
{
|
||||
InitializeCriticalSection(&criticalSection_);
|
||||
}
|
||||
|
||||
~Impl()
|
||||
{
|
||||
DeleteCriticalSection(&criticalSection_);
|
||||
}
|
||||
|
||||
void enter()
|
||||
{
|
||||
EnterCriticalSection(&criticalSection_);
|
||||
}
|
||||
|
||||
void leave()
|
||||
{
|
||||
LeaveCriticalSection(&criticalSection_);
|
||||
}
|
||||
|
||||
private:
|
||||
CRITICAL_SECTION criticalSection_;
|
||||
};
|
||||
#else
|
||||
class cv::gpu::detail::CriticalSection::Impl
|
||||
{
|
||||
public:
|
||||
Impl()
|
||||
{
|
||||
pthread_mutexattr_t mutex_attribute;
|
||||
pthread_mutexattr_init(&mutex_attribute);
|
||||
pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&mutex_, 0);
|
||||
pthread_mutexattr_destroy(&mutex_attribute);
|
||||
}
|
||||
|
||||
~Impl()
|
||||
{
|
||||
pthread_mutex_destroy(&mutex_);
|
||||
}
|
||||
|
||||
void enter()
|
||||
{
|
||||
pthread_mutex_lock(&mutex_);
|
||||
}
|
||||
|
||||
void leave()
|
||||
{
|
||||
pthread_mutex_unlock(&mutex_);
|
||||
}
|
||||
|
||||
private:
|
||||
pthread_mutex_t mutex_;
|
||||
};
|
||||
#endif
|
||||
|
||||
cv::gpu::detail::CriticalSection::CriticalSection() :
|
||||
impl_(new Impl)
|
||||
{
|
||||
}
|
||||
|
||||
cv::gpu::detail::CriticalSection::~CriticalSection()
|
||||
{
|
||||
}
|
||||
|
||||
void cv::gpu::detail::CriticalSection::enter()
|
||||
{
|
||||
impl_->enter();
|
||||
}
|
||||
|
||||
void cv::gpu::detail::CriticalSection::leave()
|
||||
{
|
||||
impl_->leave();
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
namespace
|
||||
{
|
||||
struct UserData
|
||||
{
|
||||
void (*func)(void* userData);
|
||||
void* param;
|
||||
};
|
||||
|
||||
DWORD WINAPI WinThreadFunction(LPVOID lpParam)
|
||||
{
|
||||
UserData* userData = static_cast<UserData*>(lpParam);
|
||||
|
||||
userData->func(userData->param);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
class cv::gpu::detail::Thread::Impl
|
||||
{
|
||||
public:
|
||||
Impl(void (*func)(void* userData), void* userData)
|
||||
{
|
||||
userData_.func = func;
|
||||
userData_.param = userData;
|
||||
|
||||
thread_ = CreateThread(
|
||||
NULL, // default security attributes
|
||||
0, // use default stack size
|
||||
WinThreadFunction, // thread function name
|
||||
&userData_, // argument to thread function
|
||||
0, // use default creation flags
|
||||
&threadId_); // returns the thread identifier
|
||||
}
|
||||
|
||||
~Impl()
|
||||
{
|
||||
CloseHandle(thread_);
|
||||
}
|
||||
|
||||
void wait()
|
||||
{
|
||||
WaitForSingleObject(thread_, INFINITE);
|
||||
}
|
||||
|
||||
private:
|
||||
UserData userData_;
|
||||
HANDLE thread_;
|
||||
DWORD threadId_;
|
||||
};
|
||||
#else
|
||||
namespace
|
||||
{
|
||||
struct UserData
|
||||
{
|
||||
void (*func)(void* userData);
|
||||
void* param;
|
||||
};
|
||||
|
||||
void* PThreadFunction(void* lpParam)
|
||||
{
|
||||
UserData* userData = static_cast<UserData*>(lpParam);
|
||||
|
||||
userData->func(userData->param);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
class cv::gpu::detail::Thread::Impl
|
||||
{
|
||||
public:
|
||||
Impl(void (*func)(void* userData), void* userData)
|
||||
{
|
||||
userData_.func = func;
|
||||
userData_.param = userData;
|
||||
|
||||
pthread_create(&thread_, NULL, PThreadFunction, &userData_);
|
||||
}
|
||||
|
||||
~Impl()
|
||||
{
|
||||
pthread_detach(thread_);
|
||||
}
|
||||
|
||||
void wait()
|
||||
{
|
||||
pthread_join(thread_, NULL);
|
||||
}
|
||||
|
||||
private:
|
||||
pthread_t thread_;
|
||||
UserData userData_;
|
||||
};
|
||||
#endif
|
||||
|
||||
cv::gpu::detail::Thread::Thread(void (*func)(void* userData), void* userData) :
|
||||
impl_(new Impl(func, userData))
|
||||
{
|
||||
}
|
||||
|
||||
cv::gpu::detail::Thread::~Thread()
|
||||
{
|
||||
}
|
||||
|
||||
void cv::gpu::detail::Thread::wait()
|
||||
{
|
||||
impl_->wait();
|
||||
}
|
||||
|
||||
void cv::gpu::detail::Thread::sleep(int ms)
|
||||
{
|
||||
#ifdef WIN32
|
||||
::Sleep(ms);
|
||||
#else
|
||||
::usleep(ms * 1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // HAVE_CUDA
|
@@ -1,112 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#ifndef __THREAD_WRAPPERS_H__
|
||||
#define __THREAD_WRAPPERS_H__
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
namespace cv { namespace gpu
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
class CriticalSection
|
||||
{
|
||||
public:
|
||||
CriticalSection();
|
||||
~CriticalSection();
|
||||
|
||||
void enter();
|
||||
void leave();
|
||||
|
||||
class AutoLock
|
||||
{
|
||||
public:
|
||||
explicit AutoLock(CriticalSection& criticalSection) :
|
||||
criticalSection_(criticalSection)
|
||||
{
|
||||
criticalSection_.enter();
|
||||
}
|
||||
|
||||
~AutoLock()
|
||||
{
|
||||
criticalSection_.leave();
|
||||
}
|
||||
|
||||
private:
|
||||
CriticalSection& criticalSection_;
|
||||
};
|
||||
|
||||
private:
|
||||
CriticalSection(const CriticalSection&);
|
||||
CriticalSection& operator=(const CriticalSection&);
|
||||
|
||||
class Impl;
|
||||
std::auto_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
class Thread
|
||||
{
|
||||
public:
|
||||
explicit Thread(void (*func)(void* userData), void* userData = 0);
|
||||
~Thread();
|
||||
|
||||
void wait();
|
||||
|
||||
static void sleep(int ms);
|
||||
|
||||
private:
|
||||
Thread(const Thread&);
|
||||
Thread& operator=(const Thread&);
|
||||
|
||||
class Impl;
|
||||
std::auto_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
}
|
||||
}}
|
||||
|
||||
#endif // HAVE_CUDA
|
||||
|
||||
#endif // __THREAD_WRAPPERS_H__
|
@@ -1,116 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "video_decoder.h"
|
||||
#include "frame_queue.h"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
void cv::gpu::detail::VideoDecoder::create(const VideoReader_GPU::FormatInfo& videoFormat)
|
||||
{
|
||||
release();
|
||||
|
||||
cudaVideoCodec _codec = static_cast<cudaVideoCodec>(videoFormat.codec);
|
||||
cudaVideoChromaFormat _chromaFormat = static_cast<cudaVideoChromaFormat>(videoFormat.chromaFormat);
|
||||
|
||||
cudaVideoCreateFlags videoCreateFlags = (_codec == cudaVideoCodec_JPEG || _codec == cudaVideoCodec_MPEG2) ?
|
||||
cudaVideoCreate_PreferCUDA :
|
||||
cudaVideoCreate_PreferCUVID;
|
||||
|
||||
// Validate video format. These are the currently supported formats via NVCUVID
|
||||
CV_Assert(cudaVideoCodec_MPEG1 == _codec ||
|
||||
cudaVideoCodec_MPEG2 == _codec ||
|
||||
cudaVideoCodec_MPEG4 == _codec ||
|
||||
cudaVideoCodec_VC1 == _codec ||
|
||||
cudaVideoCodec_H264 == _codec ||
|
||||
cudaVideoCodec_JPEG == _codec ||
|
||||
cudaVideoCodec_YUV420== _codec ||
|
||||
cudaVideoCodec_YV12 == _codec ||
|
||||
cudaVideoCodec_NV12 == _codec ||
|
||||
cudaVideoCodec_YUYV == _codec ||
|
||||
cudaVideoCodec_UYVY == _codec );
|
||||
|
||||
CV_Assert(cudaVideoChromaFormat_Monochrome == _chromaFormat ||
|
||||
cudaVideoChromaFormat_420 == _chromaFormat ||
|
||||
cudaVideoChromaFormat_422 == _chromaFormat ||
|
||||
cudaVideoChromaFormat_444 == _chromaFormat);
|
||||
|
||||
// Fill the decoder-create-info struct from the given video-format struct.
|
||||
std::memset(&createInfo_, 0, sizeof(CUVIDDECODECREATEINFO));
|
||||
|
||||
// Create video decoder
|
||||
createInfo_.CodecType = _codec;
|
||||
createInfo_.ulWidth = videoFormat.width;
|
||||
createInfo_.ulHeight = videoFormat.height;
|
||||
createInfo_.ulNumDecodeSurfaces = FrameQueue::MaximumSize;
|
||||
|
||||
// Limit decode memory to 24MB (16M pixels at 4:2:0 = 24M bytes)
|
||||
while (createInfo_.ulNumDecodeSurfaces * videoFormat.width * videoFormat.height > 16 * 1024 * 1024)
|
||||
createInfo_.ulNumDecodeSurfaces--;
|
||||
|
||||
createInfo_.ChromaFormat = _chromaFormat;
|
||||
createInfo_.OutputFormat = cudaVideoSurfaceFormat_NV12;
|
||||
createInfo_.DeinterlaceMode = cudaVideoDeinterlaceMode_Adaptive;
|
||||
|
||||
// No scaling
|
||||
static const int MAX_FRAME_COUNT = 2;
|
||||
|
||||
createInfo_.ulTargetWidth = createInfo_.ulWidth;
|
||||
createInfo_.ulTargetHeight = createInfo_.ulHeight;
|
||||
createInfo_.ulNumOutputSurfaces = MAX_FRAME_COUNT; // We won't simultaneously map more than 8 surfaces
|
||||
createInfo_.ulCreationFlags = videoCreateFlags;
|
||||
createInfo_.vidLock = lock_;
|
||||
|
||||
// create the decoder
|
||||
cuSafeCall( cuvidCreateDecoder(&decoder_, &createInfo_) );
|
||||
}
|
||||
|
||||
void cv::gpu::detail::VideoDecoder::release()
|
||||
{
|
||||
if (decoder_)
|
||||
{
|
||||
cuvidDestroyDecoder(decoder_);
|
||||
decoder_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // HAVE_CUDA
|
@@ -1,116 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#ifndef __VIDEO_DECODER_H__
|
||||
#define __VIDEO_DECODER_H__
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
namespace cv { namespace gpu
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
class VideoDecoder
|
||||
{
|
||||
public:
|
||||
VideoDecoder(const VideoReader_GPU::FormatInfo& videoFormat, CUvideoctxlock lock) : lock_(lock), decoder_(0)
|
||||
{
|
||||
create(videoFormat);
|
||||
}
|
||||
|
||||
~VideoDecoder()
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
void create(const VideoReader_GPU::FormatInfo& videoFormat);
|
||||
void release();
|
||||
|
||||
// Get the code-type currently used.
|
||||
cudaVideoCodec codec() const { return createInfo_.CodecType; }
|
||||
unsigned long maxDecodeSurfaces() const { return createInfo_.ulNumDecodeSurfaces; }
|
||||
|
||||
unsigned long frameWidth() const { return createInfo_.ulWidth; }
|
||||
unsigned long frameHeight() const { return createInfo_.ulHeight; }
|
||||
|
||||
unsigned long targetWidth() const { return createInfo_.ulTargetWidth; }
|
||||
unsigned long targetHeight() const { return createInfo_.ulTargetHeight; }
|
||||
|
||||
cudaVideoChromaFormat chromaFormat() const { return createInfo_.ChromaFormat; }
|
||||
|
||||
bool decodePicture(CUVIDPICPARAMS* picParams)
|
||||
{
|
||||
return cuvidDecodePicture(decoder_, picParams) == CUDA_SUCCESS;
|
||||
}
|
||||
|
||||
cv::gpu::GpuMat mapFrame(int picIdx, CUVIDPROCPARAMS& videoProcParams)
|
||||
{
|
||||
CUdeviceptr ptr;
|
||||
unsigned int pitch;
|
||||
|
||||
cuSafeCall( cuvidMapVideoFrame(decoder_, picIdx, &ptr, &pitch, &videoProcParams) );
|
||||
|
||||
return GpuMat(targetHeight() * 3 / 2, targetWidth(), CV_8UC1, (void*) ptr, pitch);
|
||||
}
|
||||
|
||||
void unmapFrame(cv::gpu::GpuMat& frame)
|
||||
{
|
||||
cuSafeCall( cuvidUnmapVideoFrame(decoder_, (CUdeviceptr) frame.data) );
|
||||
frame.release();
|
||||
}
|
||||
|
||||
private:
|
||||
VideoDecoder(const VideoDecoder&);
|
||||
VideoDecoder& operator =(const VideoDecoder&);
|
||||
|
||||
CUvideoctxlock lock_;
|
||||
CUVIDDECODECREATEINFO createInfo_;
|
||||
CUvideodecoder decoder_;
|
||||
};
|
||||
}
|
||||
}}
|
||||
|
||||
#endif // HAVE_CUDA
|
||||
|
||||
#endif // __VIDEO_DECODER_H__
|
@@ -1,161 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "video_parser.h"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
cv::gpu::detail::VideoParser::VideoParser(VideoDecoder* videoDecoder, FrameQueue* frameQueue) :
|
||||
videoDecoder_(videoDecoder), frameQueue_(frameQueue), unparsedPackets_(0), hasError_(false)
|
||||
{
|
||||
CUVIDPARSERPARAMS params;
|
||||
memset(¶ms, 0, sizeof(CUVIDPARSERPARAMS));
|
||||
|
||||
params.CodecType = videoDecoder->codec();
|
||||
params.ulMaxNumDecodeSurfaces = videoDecoder->maxDecodeSurfaces();
|
||||
params.ulMaxDisplayDelay = 1; // this flag is needed so the parser will push frames out to the decoder as quickly as it can
|
||||
params.pUserData = this;
|
||||
params.pfnSequenceCallback = HandleVideoSequence; // Called before decoding frames and/or whenever there is a format change
|
||||
params.pfnDecodePicture = HandlePictureDecode; // Called when a picture is ready to be decoded (decode order)
|
||||
params.pfnDisplayPicture = HandlePictureDisplay; // Called whenever a picture is ready to be displayed (display order)
|
||||
|
||||
cuSafeCall( cuvidCreateVideoParser(&parser_, ¶ms) );
|
||||
}
|
||||
|
||||
bool cv::gpu::detail::VideoParser::parseVideoData(const unsigned char* data, size_t size, bool endOfStream)
|
||||
{
|
||||
CUVIDSOURCEDATAPACKET packet;
|
||||
std::memset(&packet, 0, sizeof(CUVIDSOURCEDATAPACKET));
|
||||
|
||||
if (endOfStream)
|
||||
packet.flags |= CUVID_PKT_ENDOFSTREAM;
|
||||
|
||||
packet.payload_size = static_cast<unsigned long>(size);
|
||||
packet.payload = data;
|
||||
|
||||
if (cuvidParseVideoData(parser_, &packet) != CUDA_SUCCESS)
|
||||
{
|
||||
hasError_ = true;
|
||||
frameQueue_->endDecode();
|
||||
return false;
|
||||
}
|
||||
|
||||
const int maxUnparsedPackets = 15;
|
||||
|
||||
++unparsedPackets_;
|
||||
if (unparsedPackets_ > maxUnparsedPackets)
|
||||
{
|
||||
hasError_ = true;
|
||||
frameQueue_->endDecode();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (endOfStream)
|
||||
frameQueue_->endDecode();
|
||||
|
||||
return !frameQueue_->isEndOfDecode();
|
||||
}
|
||||
|
||||
int CUDAAPI cv::gpu::detail::VideoParser::HandleVideoSequence(void* userData, CUVIDEOFORMAT* format)
|
||||
{
|
||||
VideoParser* thiz = static_cast<VideoParser*>(userData);
|
||||
|
||||
thiz->unparsedPackets_ = 0;
|
||||
|
||||
if (format->codec != thiz->videoDecoder_->codec() ||
|
||||
format->coded_width != thiz->videoDecoder_->frameWidth() ||
|
||||
format->coded_height != thiz->videoDecoder_->frameHeight() ||
|
||||
format->chroma_format != thiz->videoDecoder_->chromaFormat())
|
||||
{
|
||||
VideoReader_GPU::FormatInfo newFormat;
|
||||
|
||||
newFormat.codec = static_cast<VideoReader_GPU::Codec>(format->codec);
|
||||
newFormat.chromaFormat = static_cast<VideoReader_GPU::ChromaFormat>(format->chroma_format);
|
||||
newFormat.width = format->coded_width;
|
||||
newFormat.height = format->coded_height;
|
||||
|
||||
try
|
||||
{
|
||||
thiz->videoDecoder_->create(newFormat);
|
||||
}
|
||||
catch (const cv::Exception&)
|
||||
{
|
||||
thiz->hasError_ = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int CUDAAPI cv::gpu::detail::VideoParser::HandlePictureDecode(void* userData, CUVIDPICPARAMS* picParams)
|
||||
{
|
||||
VideoParser* thiz = static_cast<VideoParser*>(userData);
|
||||
|
||||
thiz->unparsedPackets_ = 0;
|
||||
|
||||
bool isFrameAvailable = thiz->frameQueue_->waitUntilFrameAvailable(picParams->CurrPicIdx);
|
||||
|
||||
if (!isFrameAvailable)
|
||||
return false;
|
||||
|
||||
if (!thiz->videoDecoder_->decodePicture(picParams))
|
||||
{
|
||||
thiz->hasError_ = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int CUDAAPI cv::gpu::detail::VideoParser::HandlePictureDisplay(void* userData, CUVIDPARSERDISPINFO* picParams)
|
||||
{
|
||||
VideoParser* thiz = static_cast<VideoParser*>(userData);
|
||||
|
||||
thiz->unparsedPackets_ = 0;
|
||||
|
||||
thiz->frameQueue_->enqueue(picParams);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // HAVE_CUDA
|
@@ -1,100 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#ifndef __VIDEO_PARSER_H__
|
||||
#define __VIDEO_PARSER_H__
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
#include "frame_queue.h"
|
||||
#include "video_decoder.h"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
namespace cv { namespace gpu
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
class VideoParser
|
||||
{
|
||||
public:
|
||||
VideoParser(VideoDecoder* videoDecoder, FrameQueue* frameQueue);
|
||||
|
||||
~VideoParser()
|
||||
{
|
||||
cuvidDestroyVideoParser(parser_);
|
||||
}
|
||||
|
||||
bool parseVideoData(const unsigned char* data, size_t size, bool endOfStream);
|
||||
|
||||
bool hasError() const { return hasError_; }
|
||||
|
||||
private:
|
||||
VideoParser(const VideoParser&);
|
||||
VideoParser& operator =(const VideoParser&);
|
||||
|
||||
VideoDecoder* videoDecoder_;
|
||||
FrameQueue* frameQueue_;
|
||||
CUvideoparser parser_;
|
||||
int unparsedPackets_;
|
||||
volatile bool hasError_;
|
||||
|
||||
// Called when the decoder encounters a video format change (or initial sequence header)
|
||||
// This particular implementation of the callback returns 0 in case the video format changes
|
||||
// to something different than the original format. Returning 0 causes a stop of the app.
|
||||
static int CUDAAPI HandleVideoSequence(void* pUserData, CUVIDEOFORMAT* pFormat);
|
||||
|
||||
// Called by the video parser to decode a single picture
|
||||
// Since the parser will deliver data as fast as it can, we need to make sure that the picture
|
||||
// index we're attempting to use for decode is no longer used for display
|
||||
static int CUDAAPI HandlePictureDecode(void* pUserData, CUVIDPICPARAMS* pPicParams);
|
||||
|
||||
// Called by the video parser to display a video frame (in the case of field pictures, there may be
|
||||
// 2 decode calls per 1 display call, since two fields make up one frame)
|
||||
static int CUDAAPI HandlePictureDisplay(void* pUserData, CUVIDPARSERDISPINFO* pPicParams);
|
||||
};
|
||||
}
|
||||
}}
|
||||
|
||||
#endif // HAVE_CUDA
|
||||
|
||||
#endif // __VIDEO_PARSER_H__
|
@@ -1,399 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
#if !defined(HAVE_CUDA) || defined(CUDA_DISABLER) || !defined(HAVE_NVCUVID)
|
||||
|
||||
class cv::gpu::VideoReader_GPU::Impl
|
||||
{
|
||||
};
|
||||
|
||||
cv::gpu::VideoReader_GPU::VideoReader_GPU() { throw_no_cuda(); }
|
||||
cv::gpu::VideoReader_GPU::VideoReader_GPU(const String&) { throw_no_cuda(); }
|
||||
cv::gpu::VideoReader_GPU::VideoReader_GPU(const cv::Ptr<VideoSource>&) { throw_no_cuda(); }
|
||||
cv::gpu::VideoReader_GPU::~VideoReader_GPU() { }
|
||||
void cv::gpu::VideoReader_GPU::open(const String&) { throw_no_cuda(); }
|
||||
void cv::gpu::VideoReader_GPU::open(const cv::Ptr<VideoSource>&) { throw_no_cuda(); }
|
||||
bool cv::gpu::VideoReader_GPU::isOpened() const { return false; }
|
||||
void cv::gpu::VideoReader_GPU::close() { }
|
||||
bool cv::gpu::VideoReader_GPU::read(GpuMat&) { throw_no_cuda(); return false; }
|
||||
cv::gpu::VideoReader_GPU::FormatInfo cv::gpu::VideoReader_GPU::format() const { throw_no_cuda(); FormatInfo format_ = {MPEG1,Monochrome,0,0}; return format_; }
|
||||
bool cv::gpu::VideoReader_GPU::VideoSource::parseVideoData(const unsigned char*, size_t, bool) { throw_no_cuda(); return false; }
|
||||
void cv::gpu::VideoReader_GPU::dumpFormat(std::ostream&) { throw_no_cuda(); }
|
||||
|
||||
#else // HAVE_CUDA
|
||||
|
||||
#include "frame_queue.h"
|
||||
#include "video_decoder.h"
|
||||
#include "video_parser.h"
|
||||
|
||||
#include "cuvid_video_source.h"
|
||||
#include "ffmpeg_video_source.h"
|
||||
|
||||
class cv::gpu::VideoReader_GPU::Impl
|
||||
{
|
||||
public:
|
||||
explicit Impl(const cv::Ptr<cv::gpu::VideoReader_GPU::VideoSource>& source);
|
||||
~Impl();
|
||||
|
||||
bool grab(cv::gpu::GpuMat& frame);
|
||||
|
||||
cv::gpu::VideoReader_GPU::FormatInfo format() const { return videoSource_->format(); }
|
||||
|
||||
private:
|
||||
Impl(const Impl&);
|
||||
Impl& operator =(const Impl&);
|
||||
|
||||
cv::Ptr<cv::gpu::VideoReader_GPU::VideoSource> videoSource_;
|
||||
|
||||
std::auto_ptr<cv::gpu::detail::FrameQueue> frameQueue_;
|
||||
std::auto_ptr<cv::gpu::detail::VideoDecoder> videoDecoder_;
|
||||
std::auto_ptr<cv::gpu::detail::VideoParser> videoParser_;
|
||||
|
||||
CUvideoctxlock lock_;
|
||||
|
||||
std::deque< std::pair<CUVIDPARSERDISPINFO, CUVIDPROCPARAMS> > frames_;
|
||||
};
|
||||
|
||||
cv::gpu::VideoReader_GPU::Impl::Impl(const cv::Ptr<VideoSource>& source) :
|
||||
videoSource_(source),
|
||||
lock_(0)
|
||||
{
|
||||
// init context
|
||||
GpuMat temp(1, 1, CV_8UC1);
|
||||
temp.release();
|
||||
|
||||
DeviceInfo devInfo;
|
||||
CV_Assert( devInfo.supports(FEATURE_SET_COMPUTE_11) );
|
||||
|
||||
CUcontext ctx;
|
||||
cuSafeCall( cuCtxGetCurrent(&ctx) );
|
||||
cuSafeCall( cuvidCtxLockCreate(&lock_, ctx) );
|
||||
|
||||
frameQueue_.reset(new detail::FrameQueue);
|
||||
videoDecoder_.reset(new detail::VideoDecoder(videoSource_->format(), lock_));
|
||||
videoParser_.reset(new detail::VideoParser(videoDecoder_.get(), frameQueue_.get()));
|
||||
|
||||
videoSource_->setFrameQueue(frameQueue_.get());
|
||||
videoSource_->setVideoParser(videoParser_.get());
|
||||
|
||||
videoSource_->start();
|
||||
}
|
||||
|
||||
cv::gpu::VideoReader_GPU::Impl::~Impl()
|
||||
{
|
||||
frameQueue_->endDecode();
|
||||
videoSource_->stop();
|
||||
}
|
||||
|
||||
namespace cv { namespace gpu { namespace cudev {
|
||||
namespace video_decoding
|
||||
{
|
||||
void loadHueCSC(float hueCSC[9]);
|
||||
void NV12ToARGB_gpu(const PtrStepb decodedFrame, PtrStepSz<unsigned int> interopFrame, cudaStream_t stream = 0);
|
||||
}
|
||||
}}}
|
||||
|
||||
namespace
|
||||
{
|
||||
class VideoCtxAutoLock
|
||||
{
|
||||
public:
|
||||
VideoCtxAutoLock(CUvideoctxlock lock) : m_lock(lock) { cuSafeCall( cuvidCtxLock(m_lock, 0) ); }
|
||||
~VideoCtxAutoLock() { cuvidCtxUnlock(m_lock, 0); }
|
||||
|
||||
private:
|
||||
CUvideoctxlock m_lock;
|
||||
};
|
||||
|
||||
enum ColorSpace
|
||||
{
|
||||
ITU601 = 1,
|
||||
ITU709 = 2
|
||||
};
|
||||
|
||||
void setColorSpaceMatrix(ColorSpace CSC, float hueCSC[9], float hue)
|
||||
{
|
||||
float hueSin = std::sin(hue);
|
||||
float hueCos = std::cos(hue);
|
||||
|
||||
if (CSC == ITU601)
|
||||
{
|
||||
//CCIR 601
|
||||
hueCSC[0] = 1.1644f;
|
||||
hueCSC[1] = hueSin * 1.5960f;
|
||||
hueCSC[2] = hueCos * 1.5960f;
|
||||
hueCSC[3] = 1.1644f;
|
||||
hueCSC[4] = (hueCos * -0.3918f) - (hueSin * 0.8130f);
|
||||
hueCSC[5] = (hueSin * 0.3918f) - (hueCos * 0.8130f);
|
||||
hueCSC[6] = 1.1644f;
|
||||
hueCSC[7] = hueCos * 2.0172f;
|
||||
hueCSC[8] = hueSin * -2.0172f;
|
||||
}
|
||||
else if (CSC == ITU709)
|
||||
{
|
||||
//CCIR 709
|
||||
hueCSC[0] = 1.0f;
|
||||
hueCSC[1] = hueSin * 1.57480f;
|
||||
hueCSC[2] = hueCos * 1.57480f;
|
||||
hueCSC[3] = 1.0;
|
||||
hueCSC[4] = (hueCos * -0.18732f) - (hueSin * 0.46812f);
|
||||
hueCSC[5] = (hueSin * 0.18732f) - (hueCos * 0.46812f);
|
||||
hueCSC[6] = 1.0f;
|
||||
hueCSC[7] = hueCos * 1.85560f;
|
||||
hueCSC[8] = hueSin * -1.85560f;
|
||||
}
|
||||
}
|
||||
|
||||
void cudaPostProcessFrame(const cv::gpu::GpuMat& decodedFrame, cv::gpu::GpuMat& interopFrame, int width, int height)
|
||||
{
|
||||
using namespace cv::gpu::cudev::video_decoding;
|
||||
|
||||
static bool updateCSC = true;
|
||||
static float hueColorSpaceMat[9];
|
||||
|
||||
// Upload the Color Space Conversion Matrices
|
||||
if (updateCSC)
|
||||
{
|
||||
const ColorSpace colorSpace = ITU601;
|
||||
const float hue = 0.0f;
|
||||
|
||||
// CCIR 601/709
|
||||
setColorSpaceMatrix(colorSpace, hueColorSpaceMat, hue);
|
||||
|
||||
updateCSC = false;
|
||||
}
|
||||
|
||||
// Final Stage: NV12toARGB color space conversion
|
||||
|
||||
interopFrame.create(height, width, CV_8UC4);
|
||||
|
||||
loadHueCSC(hueColorSpaceMat);
|
||||
|
||||
NV12ToARGB_gpu(decodedFrame, interopFrame);
|
||||
}
|
||||
}
|
||||
|
||||
bool cv::gpu::VideoReader_GPU::Impl::grab(GpuMat& frame)
|
||||
{
|
||||
if (videoSource_->hasError() || videoParser_->hasError())
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported video source");
|
||||
|
||||
if (!videoSource_->isStarted() || frameQueue_->isEndOfDecode())
|
||||
return false;
|
||||
|
||||
if (frames_.empty())
|
||||
{
|
||||
CUVIDPARSERDISPINFO displayInfo;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (frameQueue_->dequeue(displayInfo))
|
||||
break;
|
||||
|
||||
if (videoSource_->hasError() || videoParser_->hasError())
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported video source");
|
||||
|
||||
if (frameQueue_->isEndOfDecode())
|
||||
return false;
|
||||
|
||||
// Wait a bit
|
||||
detail::Thread::sleep(1);
|
||||
}
|
||||
|
||||
bool isProgressive = displayInfo.progressive_frame != 0;
|
||||
const int num_fields = isProgressive ? 1 : 2 + displayInfo.repeat_first_field;
|
||||
|
||||
for (int active_field = 0; active_field < num_fields; ++active_field)
|
||||
{
|
||||
CUVIDPROCPARAMS videoProcParams;
|
||||
std::memset(&videoProcParams, 0, sizeof(CUVIDPROCPARAMS));
|
||||
|
||||
videoProcParams.progressive_frame = displayInfo.progressive_frame;
|
||||
videoProcParams.second_field = active_field;
|
||||
videoProcParams.top_field_first = displayInfo.top_field_first;
|
||||
videoProcParams.unpaired_field = (num_fields == 1);
|
||||
|
||||
frames_.push_back(std::make_pair(displayInfo, videoProcParams));
|
||||
}
|
||||
}
|
||||
|
||||
if (frames_.empty())
|
||||
return false;
|
||||
|
||||
std::pair<CUVIDPARSERDISPINFO, CUVIDPROCPARAMS> frameInfo = frames_.front();
|
||||
frames_.pop_front();
|
||||
|
||||
{
|
||||
VideoCtxAutoLock autoLock(lock_);
|
||||
|
||||
// map decoded video frame to CUDA surface
|
||||
cv::gpu::GpuMat decodedFrame = videoDecoder_->mapFrame(frameInfo.first.picture_index, frameInfo.second);
|
||||
|
||||
// perform post processing on the CUDA surface (performs colors space conversion and post processing)
|
||||
// comment this out if we inclue the line of code seen above
|
||||
cudaPostProcessFrame(decodedFrame, frame, videoDecoder_->targetWidth(), videoDecoder_->targetHeight());
|
||||
|
||||
// unmap video frame
|
||||
// unmapFrame() synchronizes with the VideoDecode API (ensures the frame has finished decoding)
|
||||
videoDecoder_->unmapFrame(decodedFrame);
|
||||
}
|
||||
|
||||
// release the frame, so it can be re-used in decoder
|
||||
if (frames_.empty())
|
||||
frameQueue_->releaseFrame(frameInfo.first);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
cv::gpu::VideoReader_GPU::VideoReader_GPU()
|
||||
{
|
||||
}
|
||||
|
||||
cv::gpu::VideoReader_GPU::VideoReader_GPU(const String& filename)
|
||||
{
|
||||
open(filename);
|
||||
}
|
||||
|
||||
cv::gpu::VideoReader_GPU::VideoReader_GPU(const cv::Ptr<VideoSource>& source)
|
||||
{
|
||||
open(source);
|
||||
}
|
||||
|
||||
cv::gpu::VideoReader_GPU::~VideoReader_GPU()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
void cv::gpu::VideoReader_GPU::open(const String& filename)
|
||||
{
|
||||
CV_Assert( !filename.empty() );
|
||||
|
||||
#ifndef __APPLE__
|
||||
try
|
||||
{
|
||||
cv::Ptr<VideoSource> source(new detail::CuvidVideoSource(filename));
|
||||
open(source);
|
||||
}
|
||||
catch (const std::runtime_error&)
|
||||
#endif
|
||||
{
|
||||
cv::Ptr<VideoSource> source(new cv::gpu::detail::FFmpegVideoSource(filename));
|
||||
open(source);
|
||||
}
|
||||
}
|
||||
|
||||
void cv::gpu::VideoReader_GPU::open(const cv::Ptr<VideoSource>& source)
|
||||
{
|
||||
CV_Assert( !source.empty() );
|
||||
close();
|
||||
impl_.reset(new Impl(source));
|
||||
}
|
||||
|
||||
bool cv::gpu::VideoReader_GPU::isOpened() const
|
||||
{
|
||||
return impl_.get() != 0;
|
||||
}
|
||||
|
||||
void cv::gpu::VideoReader_GPU::close()
|
||||
{
|
||||
impl_.reset();
|
||||
}
|
||||
|
||||
bool cv::gpu::VideoReader_GPU::read(GpuMat& image)
|
||||
{
|
||||
if (!isOpened())
|
||||
return false;
|
||||
|
||||
if (!impl_->grab(image))
|
||||
{
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
cv::gpu::VideoReader_GPU::FormatInfo cv::gpu::VideoReader_GPU::format() const
|
||||
{
|
||||
CV_Assert( isOpened() );
|
||||
return impl_->format();
|
||||
}
|
||||
|
||||
bool cv::gpu::VideoReader_GPU::VideoSource::parseVideoData(const unsigned char* data, size_t size, bool endOfStream)
|
||||
{
|
||||
return videoParser_->parseVideoData(data, size, endOfStream);
|
||||
}
|
||||
|
||||
void cv::gpu::VideoReader_GPU::dumpFormat(std::ostream& st)
|
||||
{
|
||||
static const char* codecs[] =
|
||||
{
|
||||
"MPEG1",
|
||||
"MPEG2",
|
||||
"MPEG4",
|
||||
"VC1",
|
||||
"H264",
|
||||
"JPEG",
|
||||
"H264_SVC",
|
||||
"H264_MVC"
|
||||
};
|
||||
|
||||
static const char* chromas[] =
|
||||
{
|
||||
"Monochrome",
|
||||
"YUV420",
|
||||
"YUV422",
|
||||
"YUV444"
|
||||
};
|
||||
|
||||
FormatInfo _format = this->format();
|
||||
|
||||
st << "Frame Size : " << _format.width << "x" << _format.height << std::endl;
|
||||
st << "Codec : " << (_format.codec <= H264_MVC ? codecs[_format.codec] : "Uncompressed YUV") << std::endl;
|
||||
st << "Chroma Format : " << chromas[_format.chromaFormat] << std::endl;
|
||||
}
|
||||
|
||||
#endif // HAVE_CUDA
|
File diff suppressed because it is too large
Load Diff
@@ -1,153 +0,0 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
#if defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// VideoReader
|
||||
|
||||
PARAM_TEST_CASE(VideoReader, cv::gpu::DeviceInfo, std::string)
|
||||
{
|
||||
cv::gpu::DeviceInfo devInfo;
|
||||
std::string inputFile;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
devInfo = GET_PARAM(0);
|
||||
inputFile = GET_PARAM(1);
|
||||
|
||||
cv::gpu::setDevice(devInfo.deviceID());
|
||||
|
||||
inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "video/" + inputFile;
|
||||
}
|
||||
};
|
||||
|
||||
GPU_TEST_P(VideoReader, Regression)
|
||||
{
|
||||
cv::gpu::VideoReader_GPU reader(inputFile);
|
||||
ASSERT_TRUE(reader.isOpened());
|
||||
|
||||
cv::gpu::GpuMat frame;
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
ASSERT_TRUE(reader.read(frame));
|
||||
ASSERT_FALSE(frame.empty());
|
||||
}
|
||||
|
||||
reader.close();
|
||||
ASSERT_FALSE(reader.isOpened());
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(GPU_Video, VideoReader, testing::Combine(
|
||||
ALL_DEVICES,
|
||||
testing::Values(std::string("768x576.avi"), std::string("1920x1080.avi"))));
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// VideoWriter
|
||||
|
||||
#ifdef WIN32
|
||||
|
||||
PARAM_TEST_CASE(VideoWriter, cv::gpu::DeviceInfo, std::string)
|
||||
{
|
||||
cv::gpu::DeviceInfo devInfo;
|
||||
std::string inputFile;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
devInfo = GET_PARAM(0);
|
||||
inputFile = GET_PARAM(1);
|
||||
|
||||
cv::gpu::setDevice(devInfo.deviceID());
|
||||
|
||||
inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + std::string("video/") + inputFile;
|
||||
}
|
||||
};
|
||||
|
||||
GPU_TEST_P(VideoWriter, Regression)
|
||||
{
|
||||
std::string outputFile = cv::tempfile(".avi");
|
||||
const double FPS = 25.0;
|
||||
|
||||
cv::VideoCapture reader(inputFile);
|
||||
ASSERT_TRUE(reader.isOpened());
|
||||
|
||||
cv::gpu::VideoWriter_GPU d_writer;
|
||||
|
||||
cv::Mat frame;
|
||||
cv::gpu::GpuMat d_frame;
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
reader >> frame;
|
||||
ASSERT_FALSE(frame.empty());
|
||||
|
||||
d_frame.upload(frame);
|
||||
|
||||
if (!d_writer.isOpened())
|
||||
d_writer.open(outputFile, frame.size(), FPS);
|
||||
|
||||
d_writer.write(d_frame);
|
||||
}
|
||||
|
||||
reader.release();
|
||||
d_writer.close();
|
||||
|
||||
reader.open(outputFile);
|
||||
ASSERT_TRUE(reader.isOpened());
|
||||
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
reader >> frame;
|
||||
ASSERT_FALSE(frame.empty());
|
||||
}
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(GPU_Video, VideoWriter, testing::Combine(
|
||||
ALL_DEVICES,
|
||||
testing::Values(std::string("768x576.avi"), std::string("1920x1080.avi"))));
|
||||
|
||||
#endif // WIN32
|
||||
|
||||
#endif // defined(HAVE_CUDA) && defined(HAVE_NVCUVID)
|
Reference in New Issue
Block a user