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#include "precomp.hpp"
45
46#ifdef HAVE_NVCUVID
47
48using namespace cv;
49using namespace cv::cudacodec;
50using namespace cv::cudacodec::detail;
51
52namespace
53{
54    Create_InputMediaStream_FFMPEG_Plugin create_InputMediaStream_FFMPEG_p = 0;
55    Release_InputMediaStream_FFMPEG_Plugin release_InputMediaStream_FFMPEG_p = 0;
56    Read_InputMediaStream_FFMPEG_Plugin read_InputMediaStream_FFMPEG_p = 0;
57
58    bool init_MediaStream_FFMPEG()
59    {
60        static bool initialized = 0;
61
62        if (!initialized)
63        {
64            #if defined WIN32 || defined _WIN32
65                const char* module_name = "opencv_ffmpeg"
66                    CVAUX_STR(CV_VERSION_MAJOR) CVAUX_STR(CV_VERSION_MINOR) CVAUX_STR(CV_VERSION_REVISION)
67                #if (defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__)
68                    "_64"
69                #endif
70                    ".dll";
71
72                static HMODULE cvFFOpenCV = LoadLibrary(module_name);
73
74                if (cvFFOpenCV)
75                {
76                    create_InputMediaStream_FFMPEG_p =
77                        (Create_InputMediaStream_FFMPEG_Plugin)GetProcAddress(cvFFOpenCV, "create_InputMediaStream_FFMPEG");
78                    release_InputMediaStream_FFMPEG_p =
79                        (Release_InputMediaStream_FFMPEG_Plugin)GetProcAddress(cvFFOpenCV, "release_InputMediaStream_FFMPEG");
80                    read_InputMediaStream_FFMPEG_p =
81                        (Read_InputMediaStream_FFMPEG_Plugin)GetProcAddress(cvFFOpenCV, "read_InputMediaStream_FFMPEG");
82
83                    initialized = create_InputMediaStream_FFMPEG_p != 0 && release_InputMediaStream_FFMPEG_p != 0 && read_InputMediaStream_FFMPEG_p != 0;
84                }
85            #elif defined HAVE_FFMPEG
86                create_InputMediaStream_FFMPEG_p = create_InputMediaStream_FFMPEG;
87                release_InputMediaStream_FFMPEG_p = release_InputMediaStream_FFMPEG;
88                read_InputMediaStream_FFMPEG_p = read_InputMediaStream_FFMPEG;
89
90                initialized = true;
91            #endif
92        }
93
94        return initialized;
95    }
96}
97
98cv::cudacodec::detail::FFmpegVideoSource::FFmpegVideoSource(const String& fname) :
99    stream_(0)
100{
101    CV_Assert( init_MediaStream_FFMPEG() );
102
103    int codec;
104    int chroma_format;
105    int width;
106    int height;
107
108    stream_ = create_InputMediaStream_FFMPEG_p(fname.c_str(), &codec, &chroma_format, &width, &height);
109    if (!stream_)
110        CV_Error(Error::StsUnsupportedFormat, "Unsupported video source");
111
112    format_.codec = static_cast<Codec>(codec);
113    format_.chromaFormat = static_cast<ChromaFormat>(chroma_format);
114    format_.width = width;
115    format_.height = height;
116}
117
118cv::cudacodec::detail::FFmpegVideoSource::~FFmpegVideoSource()
119{
120    if (stream_)
121        release_InputMediaStream_FFMPEG_p(stream_);
122}
123
124FormatInfo cv::cudacodec::detail::FFmpegVideoSource::format() const
125{
126    return format_;
127}
128
129bool cv::cudacodec::detail::FFmpegVideoSource::getNextPacket(unsigned char** data, int* size, bool* bEndOfFile)
130{
131    int endOfFile;
132
133    int res = read_InputMediaStream_FFMPEG_p(stream_, data, size, &endOfFile);
134
135    *bEndOfFile = (endOfFile != 0);
136    return res != 0;
137}
138
139#endif // HAVE_CUDA
140