1/*M///////////////////////////////////////////////////////////////////////////////////////
2//
3//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4//
5//  By downloading, copying, installing or using the software you agree to this license.
6//  If you do not agree to this license, do not download, install,
7//  copy or use the software.
8//
9//
10//                          License Agreement
11//                For Open Source Computer Vision Library
12//
13// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16// Third party copyrights are property of their respective owners.
17//
18// Redistribution and use in source and binary forms, with or without modification,
19// are permitted provided that the following conditions are met:
20//
21//   * Redistribution's of source code must retain the above copyright notice,
22//     this list of conditions and the following disclaimer.
23//
24//   * Redistribution's in binary form must reproduce the above copyright notice,
25//     this list of conditions and the following disclaimer in the documentation
26//     and/or other materials provided with the distribution.
27//
28//   * The name of the copyright holders may not be used to endorse or promote products
29//     derived from this software without specific prior written permission.
30//
31// This software is provided by the copyright holders and contributors "as is" and
32// any express or implied warranties, including, but not limited to, the implied
33// warranties of merchantability and fitness for a particular purpose are disclaimed.
34// In no event shall the Intel Corporation or contributors be liable for any direct,
35// indirect, incidental, special, exemplary, or consequential damages
36// (including, but not limited to, procurement of substitute goods or services;
37// loss of use, data, or profits; or business interruption) however caused
38// and on any theory of liability, whether in contract, strict liability,
39// or tort (including negligence or otherwise) arising in any way out of
40// the use of this software, even if advised of the possibility of such damage.
41//
42//M*/
43
44#ifndef __OPENCV_CUDACODEC_HPP__
45#define __OPENCV_CUDACODEC_HPP__
46
47#ifndef __cplusplus
48#  error cudacodec.hpp header must be compiled as C++
49#endif
50
51#include "opencv2/core/cuda.hpp"
52
53/**
54  @addtogroup cuda
55  @{
56    @defgroup cudacodec Video Encoding/Decoding
57  @}
58 */
59
60namespace cv { namespace cudacodec {
61
62//! @addtogroup cudacodec
63//! @{
64
65////////////////////////////////// Video Encoding //////////////////////////////////
66
67// Works only under Windows.
68// Supports olny H264 video codec and AVI files.
69
70enum SurfaceFormat
71{
72    SF_UYVY = 0,
73    SF_YUY2,
74    SF_YV12,
75    SF_NV12,
76    SF_IYUV,
77    SF_BGR,
78    SF_GRAY = SF_BGR
79};
80
81/** @brief Different parameters for CUDA video encoder.
82 */
83struct CV_EXPORTS EncoderParams
84{
85    int P_Interval;      //!< NVVE_P_INTERVAL,
86    int IDR_Period;      //!< NVVE_IDR_PERIOD,
87    int DynamicGOP;      //!< NVVE_DYNAMIC_GOP,
88    int RCType;          //!< NVVE_RC_TYPE,
89    int AvgBitrate;      //!< NVVE_AVG_BITRATE,
90    int PeakBitrate;     //!< NVVE_PEAK_BITRATE,
91    int QP_Level_Intra;  //!< NVVE_QP_LEVEL_INTRA,
92    int QP_Level_InterP; //!< NVVE_QP_LEVEL_INTER_P,
93    int QP_Level_InterB; //!< NVVE_QP_LEVEL_INTER_B,
94    int DeblockMode;     //!< NVVE_DEBLOCK_MODE,
95    int ProfileLevel;    //!< NVVE_PROFILE_LEVEL,
96    int ForceIntra;      //!< NVVE_FORCE_INTRA,
97    int ForceIDR;        //!< NVVE_FORCE_IDR,
98    int ClearStat;       //!< NVVE_CLEAR_STAT,
99    int DIMode;          //!< NVVE_SET_DEINTERLACE,
100    int Presets;         //!< NVVE_PRESETS,
101    int DisableCabac;    //!< NVVE_DISABLE_CABAC,
102    int NaluFramingType; //!< NVVE_CONFIGURE_NALU_FRAMING_TYPE
103    int DisableSPSPPS;   //!< NVVE_DISABLE_SPS_PPS
104
105    EncoderParams();
106    /** @brief Constructors.
107
108    @param configFile Config file name.
109
110    Creates default parameters or reads parameters from config file.
111     */
112    explicit EncoderParams(const String& configFile);
113
114    /** @brief Reads parameters from config file.
115
116    @param configFile Config file name.
117     */
118    void load(const String& configFile);
119    /** @brief Saves parameters to config file.
120
121    @param configFile Config file name.
122     */
123    void save(const String& configFile) const;
124};
125
126/** @brief Callbacks for CUDA video encoder.
127 */
128class CV_EXPORTS EncoderCallBack
129{
130public:
131    enum PicType
132    {
133        IFRAME = 1,
134        PFRAME = 2,
135        BFRAME = 3
136    };
137
138    virtual ~EncoderCallBack() {}
139
140    /** @brief Callback function to signal the start of bitstream that is to be encoded.
141
142    Callback must allocate buffer for CUDA encoder and return pointer to it and it's size.
143     */
144    virtual uchar* acquireBitStream(int* bufferSize) = 0;
145
146    /** @brief Callback function to signal that the encoded bitstream is ready to be written to file.
147    */
148    virtual void releaseBitStream(unsigned char* data, int size) = 0;
149
150    /** @brief Callback function to signal that the encoding operation on the frame has started.
151
152    @param frameNumber
153    @param picType Specify frame type (I-Frame, P-Frame or B-Frame).
154     */
155    virtual void onBeginFrame(int frameNumber, PicType picType) = 0;
156
157    /** @brief Callback function signals that the encoding operation on the frame has finished.
158
159    @param frameNumber
160    @param picType Specify frame type (I-Frame, P-Frame or B-Frame).
161     */
162    virtual void onEndFrame(int frameNumber, PicType picType) = 0;
163};
164
165/** @brief Video writer interface.
166
167The implementation uses H264 video codec.
168
169@note Currently only Windows platform is supported.
170
171@note
172   -   An example on how to use the videoWriter class can be found at
173        opencv_source_code/samples/gpu/video_writer.cpp
174 */
175class CV_EXPORTS VideoWriter
176{
177public:
178    virtual ~VideoWriter() {}
179
180    /** @brief Writes the next video frame.
181
182    @param frame The written frame.
183    @param lastFrame Indicates that it is end of stream. The parameter can be ignored.
184
185    The method write the specified image to video file. The image must have the same size and the same
186    surface format as has been specified when opening the video writer.
187     */
188    virtual void write(InputArray frame, bool lastFrame = false) = 0;
189
190    virtual EncoderParams getEncoderParams() const = 0;
191};
192
193/** @brief Creates video writer.
194
195@param fileName Name of the output video file. Only AVI file format is supported.
196@param frameSize Size of the input video frames.
197@param fps Framerate of the created video stream.
198@param format Surface format of input frames ( SF_UYVY , SF_YUY2 , SF_YV12 , SF_NV12 ,
199SF_IYUV , SF_BGR or SF_GRAY). BGR or gray frames will be converted to YV12 format before
200encoding, frames with other formats will be used as is.
201
202The constructors initialize video writer. FFMPEG is used to write videos. User can implement own
203multiplexing with cudacodec::EncoderCallBack .
204 */
205CV_EXPORTS Ptr<VideoWriter> createVideoWriter(const String& fileName, Size frameSize, double fps, SurfaceFormat format = SF_BGR);
206/** @overload
207@param fileName Name of the output video file. Only AVI file format is supported.
208@param frameSize Size of the input video frames.
209@param fps Framerate of the created video stream.
210@param params Encoder parameters. See cudacodec::EncoderParams .
211@param format Surface format of input frames ( SF_UYVY , SF_YUY2 , SF_YV12 , SF_NV12 ,
212SF_IYUV , SF_BGR or SF_GRAY). BGR or gray frames will be converted to YV12 format before
213encoding, frames with other formats will be used as is.
214*/
215CV_EXPORTS Ptr<VideoWriter> createVideoWriter(const String& fileName, Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
216
217/** @overload
218@param encoderCallback Callbacks for video encoder. See cudacodec::EncoderCallBack . Use it if you
219want to work with raw video stream.
220@param frameSize Size of the input video frames.
221@param fps Framerate of the created video stream.
222@param format Surface format of input frames ( SF_UYVY , SF_YUY2 , SF_YV12 , SF_NV12 ,
223SF_IYUV , SF_BGR or SF_GRAY). BGR or gray frames will be converted to YV12 format before
224encoding, frames with other formats will be used as is.
225*/
226CV_EXPORTS Ptr<VideoWriter> createVideoWriter(const Ptr<EncoderCallBack>& encoderCallback, Size frameSize, double fps, SurfaceFormat format = SF_BGR);
227/** @overload
228@param encoderCallback Callbacks for video encoder. See cudacodec::EncoderCallBack . Use it if you
229want to work with raw video stream.
230@param frameSize Size of the input video frames.
231@param fps Framerate of the created video stream.
232@param params Encoder parameters. See cudacodec::EncoderParams .
233@param format Surface format of input frames ( SF_UYVY , SF_YUY2 , SF_YV12 , SF_NV12 ,
234SF_IYUV , SF_BGR or SF_GRAY). BGR or gray frames will be converted to YV12 format before
235encoding, frames with other formats will be used as is.
236*/
237CV_EXPORTS Ptr<VideoWriter> createVideoWriter(const Ptr<EncoderCallBack>& encoderCallback, Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
238
239////////////////////////////////// Video Decoding //////////////////////////////////////////
240
241/** @brief Video codecs supported by cudacodec::VideoReader .
242 */
243enum Codec
244{
245    MPEG1 = 0,
246    MPEG2,
247    MPEG4,
248    VC1,
249    H264,
250    JPEG,
251    H264_SVC,
252    H264_MVC,
253
254    Uncompressed_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')),   //!< Y,U,V (4:2:0)
255    Uncompressed_YV12   = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')),   //!< Y,V,U (4:2:0)
256    Uncompressed_NV12   = (('N'<<24)|('V'<<16)|('1'<<8)|('2')),   //!< Y,UV  (4:2:0)
257    Uncompressed_YUYV   = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')),   //!< YUYV/YUY2 (4:2:2)
258    Uncompressed_UYVY   = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y'))    //!< UYVY (4:2:2)
259};
260
261/** @brief Chroma formats supported by cudacodec::VideoReader .
262 */
263enum ChromaFormat
264{
265    Monochrome = 0,
266    YUV420,
267    YUV422,
268    YUV444
269};
270
271/** @brief Struct providing information about video file format. :
272 */
273struct FormatInfo
274{
275    Codec codec;
276    ChromaFormat chromaFormat;
277    int width;
278    int height;
279};
280
281/** @brief Video reader interface.
282
283@note
284   -   An example on how to use the videoReader class can be found at
285        opencv_source_code/samples/gpu/video_reader.cpp
286 */
287class CV_EXPORTS VideoReader
288{
289public:
290    virtual ~VideoReader() {}
291
292    /** @brief Grabs, decodes and returns the next video frame.
293
294    If no frames has been grabbed (there are no more frames in video file), the methods return false .
295    The method throws Exception if error occurs.
296     */
297    virtual bool nextFrame(OutputArray frame) = 0;
298
299    /** @brief Returns information about video file format.
300    */
301    virtual FormatInfo format() const = 0;
302};
303
304/** @brief Interface for video demultiplexing. :
305
306User can implement own demultiplexing by implementing this interface.
307 */
308class CV_EXPORTS RawVideoSource
309{
310public:
311    virtual ~RawVideoSource() {}
312
313    /** @brief Returns next packet with RAW video frame.
314
315    @param data Pointer to frame data.
316    @param size Size in bytes of current frame.
317    @param endOfFile Indicates that it is end of stream.
318     */
319    virtual bool getNextPacket(unsigned char** data, int* size, bool* endOfFile) = 0;
320
321    /** @brief Returns information about video file format.
322    */
323    virtual FormatInfo format() const = 0;
324};
325
326/** @brief Creates video reader.
327
328@param filename Name of the input video file.
329
330FFMPEG is used to read videos. User can implement own demultiplexing with cudacodec::RawVideoSource
331 */
332CV_EXPORTS Ptr<VideoReader> createVideoReader(const String& filename);
333/** @overload
334@param source RAW video source implemented by user.
335*/
336CV_EXPORTS Ptr<VideoReader> createVideoReader(const Ptr<RawVideoSource>& source);
337
338//! @}
339
340}} // namespace cv { namespace cudacodec {
341
342#endif /* __OPENCV_CUDACODEC_HPP__ */
343