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 __VIDEO_PARSER_HPP__
45 #define __VIDEO_PARSER_HPP__
46 
47 #include <nvcuvid.h>
48 
49 #include "opencv2/core/private.cuda.hpp"
50 #include "opencv2/cudacodec.hpp"
51 #include "frame_queue.hpp"
52 #include "video_decoder.hpp"
53 
54 namespace cv { namespace cudacodec { namespace detail
55 {
56 
57 class VideoParser
58 {
59 public:
60     VideoParser(VideoDecoder* videoDecoder, FrameQueue* frameQueue);
61 
~VideoParser()62     ~VideoParser()
63     {
64         cuvidDestroyVideoParser(parser_);
65     }
66 
67     bool parseVideoData(const unsigned char* data, size_t size, bool endOfStream);
68 
hasError() const69     bool hasError() const { return hasError_; }
70 
71 private:
72     VideoDecoder* videoDecoder_;
73     FrameQueue* frameQueue_;
74     CUvideoparser parser_;
75     int unparsedPackets_;
76     volatile bool hasError_;
77 
78     // Called when the decoder encounters a video format change (or initial sequence header)
79     // This particular implementation of the callback returns 0 in case the video format changes
80     // to something different than the original format. Returning 0 causes a stop of the app.
81     static int CUDAAPI HandleVideoSequence(void* pUserData, CUVIDEOFORMAT* pFormat);
82 
83     // Called by the video parser to decode a single picture
84     // Since the parser will deliver data as fast as it can, we need to make sure that the picture
85     // index we're attempting to use for decode is no longer used for display
86     static int CUDAAPI HandlePictureDecode(void* pUserData, CUVIDPICPARAMS* pPicParams);
87 
88     // Called by the video parser to display a video frame (in the case of field pictures, there may be
89     // 2 decode calls per 1 display call, since two fields make up one frame)
90     static int CUDAAPI HandlePictureDisplay(void* pUserData, CUVIDPARSERDISPINFO* pPicParams);
91 };
92 
93 }}}
94 
95 #endif // __VIDEO_PARSER_HPP__
96