refactored VideoWriter class (convert it to abstract interface)

This commit is contained in:
Vladislav Vinogradov 2013-04-22 14:04:27 +04:00
parent 7a07f1a9e7
commit e9a74c17f8
7 changed files with 769 additions and 891 deletions

View File

@ -5,80 +5,25 @@ Video Encoding
gpu::VideoWriter_GPU
gpucodec::VideoWriter
---------------------
Video writer class.
Video writer interface.
.. ocv:class:: gpu::VideoWriter_GPU
.. ocv:class:: gpucodec::VideoWriter
The class uses H264 video codec.
The implementation 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
---------------------------
gpucodec::VideoWriter::write
----------------------------
Writes the next video frame.
.. ocv:function:: void gpu::VideoWriter_GPU::write(const cv::gpu::GpuMat& image, bool lastFrame = false)
.. ocv:function:: void gpucodec::VideoWriter::write(InputArray frame, bool lastFrame = false) = 0
:param image: The written frame.
:param frame: The written frame.
:param lastFrame: Indicates that it is end of stream. The parameter can be ignored.
@ -86,9 +31,34 @@ The method write the specified image to video file. The image must have the same
gpu::VideoWriter_GPU::EncoderParams
-----------------------------------
.. ocv:struct:: gpu::VideoWriter_GPU::EncoderParams
gpucodec::createVideoWriter
---------------------------
Creates video writer.
.. ocv:function:: Ptr<gpucodec::VideoWriter> gpucodec::createVideoWriter(const String& fileName, Size frameSize, double fps, SurfaceFormat format = SF_BGR)
.. ocv:function:: Ptr<gpucodec::VideoWriter> gpucodec::createVideoWriter(const String& fileName, Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR)
.. ocv:function:: Ptr<gpucodec::VideoWriter> gpucodec::createVideoWriter(const Ptr<EncoderCallBack>& encoderCallback, Size frameSize, double fps, SurfaceFormat format = SF_BGR)
.. ocv:function:: Ptr<gpucodec::VideoWriter> gpucodec::createVideoWriter(const Ptr<EncoderCallBack>& encoderCallback, 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:`gpucodec::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:`gpucodec::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:`gpucodec::EncoderCallBack` .
gpucodec::EncoderParams
-----------------------
.. ocv:struct:: gpucodec::EncoderParams
Different parameters for CUDA video encoder. ::
@ -123,12 +93,12 @@ Different parameters for CUDA video encoder. ::
gpu::VideoWriter_GPU::EncoderParams::EncoderParams
--------------------------------------------------
gpucodec::EncoderParams::EncoderParams
--------------------------------------
Constructors.
.. ocv:function:: gpu::VideoWriter_GPU::EncoderParams::EncoderParams()
.. ocv:function:: gpu::VideoWriter_GPU::EncoderParams::EncoderParams(const String& configFile)
.. ocv:function:: gpucodec::EncoderParams::EncoderParams()
.. ocv:function:: gpucodec::EncoderParams::EncoderParams(const String& configFile)
:param configFile: Config file name.
@ -136,29 +106,29 @@ Creates default parameters or reads parameters from config file.
gpu::VideoWriter_GPU::EncoderParams::load
-----------------------------------------
gpucodec::EncoderParams::load
-----------------------------
Reads parameters from config file.
.. ocv:function:: void gpu::VideoWriter_GPU::EncoderParams::load(const String& configFile)
.. ocv:function:: void gpucodec::EncoderParams::load(const String& configFile)
:param configFile: Config file name.
gpu::VideoWriter_GPU::EncoderParams::save
-----------------------------------------
gpucodec::EncoderParams::save
-----------------------------
Saves parameters to config file.
.. ocv:function:: void gpu::VideoWriter_GPU::EncoderParams::save(const String& configFile) const
.. ocv:function:: void gpucodec::EncoderParams::save(const String& configFile) const
:param configFile: Config file name.
gpu::VideoWriter_GPU::EncoderCallBack
-------------------------------------
.. ocv:class:: gpu::VideoWriter_GPU::EncoderCallBack
gpucodec::EncoderCallBack
-------------------------
.. ocv:class:: gpucodec::EncoderCallBack
Callbacks for CUDA video encoder. ::
@ -182,38 +152,38 @@ Callbacks for CUDA video encoder. ::
gpu::VideoWriter_GPU::EncoderCallBack::acquireBitStream
-------------------------------------------------------
gpucodec::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
.. ocv:function:: virtual uchar* gpucodec::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
-------------------------------------------------------
gpucodec::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
.. ocv:function:: virtual void gpucodec::EncoderCallBack::releaseBitStream(unsigned char* data, int size) = 0
gpu::VideoWriter_GPU::EncoderCallBack::onBeginFrame
---------------------------------------------------
gpucodec::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
.. ocv:function:: virtual void gpucodec::EncoderCallBack::onBeginFrame(int frameNumber, PicType picType) = 0
:param picType: Specify frame type (I-Frame, P-Frame or B-Frame).
gpu::VideoWriter_GPU::EncoderCallBack::onEndFrame
-------------------------------------------------
gpucodec::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
.. ocv:function:: virtual void gpucodec::EncoderCallBack::onEndFrame(int frameNumber, PicType picType) = 0
:param picType: Specify frame type (I-Frame, P-Frame or B-Frame).

View File

@ -7,11 +7,12 @@
// copy or use the software.
//
//
// License Agreement
// 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.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
@ -51,112 +52,102 @@
#include "opencv2/core/gpu.hpp"
namespace cv { namespace gpu {
namespace cv { namespace gpucodec {
////////////////////////////////// Video Encoding //////////////////////////////////
// Works only under Windows
// Supports olny H264 video codec and AVI files
class CV_EXPORTS VideoWriter_GPU
// Works only under Windows.
// Supports olny H264 video codec and AVI files.
enum SurfaceFormat
{
SF_UYVY = 0,
SF_YUY2,
SF_YV12,
SF_NV12,
SF_IYUV,
SF_BGR,
SF_GRAY = SF_BGR
};
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;
};
class CV_EXPORTS EncoderCallBack
{
public:
struct EncoderParams;
// Callbacks for video encoder, use it if you want to work with raw video stream
class EncoderCallBack;
enum SurfaceFormat
enum PicType
{
SF_UYVY = 0,
SF_YUY2,
SF_YV12,
SF_NV12,
SF_IYUV,
SF_BGR,
SF_GRAY = SF_BGR
IFRAME = 1,
PFRAME = 2,
BFRAME = 3
};
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();
virtual ~EncoderCallBack() {}
// 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);
//! callback function to signal the start of bitstream that is to be encoded
//! callback must allocate host buffer for CUDA encoder and return pointer to it and it's size
virtual uchar* acquireBitStream(int* bufferSize) = 0;
bool isOpened() const;
void close();
//! callback function to signal that the encoded bitstream is ready to be written to file
virtual void releaseBitStream(unsigned char* data, int size) = 0;
void write(const cv::gpu::GpuMat& image, bool lastFrame = false);
//! callback function to signal that the encoding operation on the frame has started
virtual void onBeginFrame(int frameNumber, PicType picType) = 0;
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;
};
class Impl;
private:
cv::Ptr<Impl> impl_;
//! callback function signals that the encoding operation on the frame has finished
virtual void onEndFrame(int frameNumber, PicType picType) = 0;
};
class CV_EXPORTS VideoWriter
{
public:
virtual ~VideoWriter() {}
//! writes the next frame from GPU memory
virtual void write(InputArray frame, bool lastFrame = false) = 0;
virtual EncoderParams getEncoderParams() const = 0;
};
//! create VideoWriter for specified output file (only AVI file format is supported)
CV_EXPORTS Ptr<VideoWriter> createVideoWriter(const String& fileName, Size frameSize, double fps, SurfaceFormat format = SF_BGR);
CV_EXPORTS Ptr<VideoWriter> createVideoWriter(const String& fileName, Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
//! create VideoWriter for user-defined callbacks
CV_EXPORTS Ptr<VideoWriter> createVideoWriter(const Ptr<EncoderCallBack>& encoderCallback, Size frameSize, double fps, SurfaceFormat format = SF_BGR);
CV_EXPORTS Ptr<VideoWriter> createVideoWriter(const Ptr<EncoderCallBack>& encoderCallback, Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
}} // namespace cv { namespace gpucodec {
namespace cv { namespace gpu {
////////////////////////////////// Video Decoding //////////////////////////////////////////
namespace detail
@ -257,7 +248,6 @@ private:
namespace cv {
template <> CV_EXPORTS void Ptr<cv::gpu::VideoWriter_GPU::Impl>::delete_obj();
template <> CV_EXPORTS void Ptr<cv::gpu::VideoReader_GPU::Impl>::delete_obj();
}

View File

@ -119,7 +119,7 @@ PERF_TEST_P(FileName, VideoWriter, Values("gpu/video/768x576.avi", "gpu/video/19
if (PERF_RUN_GPU())
{
cv::gpu::VideoWriter_GPU d_writer;
cv::Ptr<cv::gpucodec::VideoWriter> d_writer;
cv::gpu::GpuMat d_frame;
@ -130,11 +130,11 @@ PERF_TEST_P(FileName, VideoWriter, Values("gpu/video/768x576.avi", "gpu/video/19
d_frame.upload(frame);
if (!d_writer.isOpened())
d_writer.open(outputFile, frame.size(), FPS);
if (d_writer.empty())
d_writer = cv::gpucodec::createVideoWriter(outputFile, frame.size(), FPS);
startTimer(); next();
d_writer.write(d_frame);
d_writer->write(d_frame);
stopTimer();
}
}

View File

@ -7,11 +7,12 @@
// copy or use the software.
//
//
// License Agreement
// 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.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,

File diff suppressed because it is too large Load Diff

View File

@ -89,7 +89,7 @@ GPU_TEST_P(Video, Writer)
cv::VideoCapture reader(inputFile);
ASSERT_TRUE(reader.isOpened());
cv::gpu::VideoWriter_GPU d_writer;
cv::Ptr<cv::gpucodec::VideoWriter> d_writer;
cv::Mat frame;
cv::gpu::GpuMat d_frame;
@ -101,14 +101,14 @@ GPU_TEST_P(Video, Writer)
d_frame.upload(frame);
if (!d_writer.isOpened())
d_writer.open(outputFile, frame.size(), FPS);
if (d_writer.empty())
d_writer = cv::gpucodec::createVideoWriter(outputFile, frame.size(), FPS);
d_writer.write(d_frame);
d_writer->write(d_frame);
}
reader.release();
d_writer.close();
d_writer.release();
reader.open(outputFile);
ASSERT_TRUE(reader.isOpened());

View File

@ -33,7 +33,7 @@ int main(int argc, const char* argv[])
cv::gpu::printShortCudaDeviceInfo(cv::gpu::getDevice());
cv::VideoWriter writer;
cv::gpu::VideoWriter_GPU d_writer;
cv::Ptr<cv::gpucodec::VideoWriter> d_writer;
cv::Mat frame;
cv::gpu::GpuMat d_frame;
@ -64,11 +64,11 @@ int main(int argc, const char* argv[])
return -1;
}
if (!d_writer.isOpened())
if (d_writer.empty())
{
std::cout << "Open GPU Writer" << std::endl;
d_writer.open("output_gpu.avi", frame.size(), FPS);
d_writer = cv::gpucodec::createVideoWriter("output_gpu.avi", frame.size(), FPS);
}
d_frame.upload(frame);
@ -81,7 +81,7 @@ int main(int argc, const char* argv[])
cpu_times.push_back(tm.getTimeMilli());
tm.reset(); tm.start();
d_writer.write(d_frame);
d_writer->write(d_frame);
tm.stop();
gpu_times.push_back(tm.getTimeMilli());
}