1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "precomp.hpp"
45 #include "cap_ffmpeg_impl.hpp"
47 #include "cap_ffmpeg_api.hpp"
50 static CvCreateFileCapture_Plugin icvCreateFileCapture_FFMPEG_p = 0;
51 static CvReleaseCapture_Plugin icvReleaseCapture_FFMPEG_p = 0;
52 static CvGrabFrame_Plugin icvGrabFrame_FFMPEG_p = 0;
53 static CvRetrieveFrame_Plugin icvRetrieveFrame_FFMPEG_p = 0;
54 static CvSetCaptureProperty_Plugin icvSetCaptureProperty_FFMPEG_p = 0;
55 static CvGetCaptureProperty_Plugin icvGetCaptureProperty_FFMPEG_p = 0;
56 static CvCreateVideoWriter_Plugin icvCreateVideoWriter_FFMPEG_p = 0;
57 static CvReleaseVideoWriter_Plugin icvReleaseVideoWriter_FFMPEG_p = 0;
58 static CvWriteFrame_Plugin icvWriteFrame_FFMPEG_p = 0;
60 static cv::Mutex _icvInitFFMPEG_mutex;
67 cv::AutoLock al(_icvInitFFMPEG_mutex);
68 static icvInitFFMPEG init;
72 #if defined WIN32 || defined _WIN32
79 FreeLibrary(icvFFOpenCV);
87 #if defined WIN32 || defined _WIN32
88 const char* module_name = "opencv_ffmpeg"
89 CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
90 #if (defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__)
95 icvFFOpenCV = LoadLibrary( module_name );
98 icvCreateFileCapture_FFMPEG_p =
99 (CvCreateFileCapture_Plugin)GetProcAddress(icvFFOpenCV, "cvCreateFileCapture_FFMPEG");
100 icvReleaseCapture_FFMPEG_p =
101 (CvReleaseCapture_Plugin)GetProcAddress(icvFFOpenCV, "cvReleaseCapture_FFMPEG");
102 icvGrabFrame_FFMPEG_p =
103 (CvGrabFrame_Plugin)GetProcAddress(icvFFOpenCV, "cvGrabFrame_FFMPEG");
104 icvRetrieveFrame_FFMPEG_p =
105 (CvRetrieveFrame_Plugin)GetProcAddress(icvFFOpenCV, "cvRetrieveFrame_FFMPEG");
106 icvSetCaptureProperty_FFMPEG_p =
107 (CvSetCaptureProperty_Plugin)GetProcAddress(icvFFOpenCV, "cvSetCaptureProperty_FFMPEG");
108 icvGetCaptureProperty_FFMPEG_p =
109 (CvGetCaptureProperty_Plugin)GetProcAddress(icvFFOpenCV, "cvGetCaptureProperty_FFMPEG");
110 icvCreateVideoWriter_FFMPEG_p =
111 (CvCreateVideoWriter_Plugin)GetProcAddress(icvFFOpenCV, "cvCreateVideoWriter_FFMPEG");
112 icvReleaseVideoWriter_FFMPEG_p =
113 (CvReleaseVideoWriter_Plugin)GetProcAddress(icvFFOpenCV, "cvReleaseVideoWriter_FFMPEG");
114 icvWriteFrame_FFMPEG_p =
115 (CvWriteFrame_Plugin)GetProcAddress(icvFFOpenCV, "cvWriteFrame_FFMPEG");
118 if( icvCreateFileCapture_FFMPEG_p != 0 &&
119 icvReleaseCapture_FFMPEG_p != 0 &&
120 icvGrabFrame_FFMPEG_p != 0 &&
121 icvRetrieveFrame_FFMPEG_p != 0 &&
122 icvSetCaptureProperty_FFMPEG_p != 0 &&
123 icvGetCaptureProperty_FFMPEG_p != 0 &&
124 icvCreateVideoWriter_FFMPEG_p != 0 &&
125 icvReleaseVideoWriter_FFMPEG_p != 0 &&
126 icvWriteFrame_FFMPEG_p != 0 )
128 printf("Successfully initialized ffmpeg plugin!\n");
132 printf("Failed to load FFMPEG plugin: module handle=%p\n", icvFFOpenCV);
136 #elif defined HAVE_FFMPEG
137 icvCreateFileCapture_FFMPEG_p = (CvCreateFileCapture_Plugin)cvCreateFileCapture_FFMPEG;
138 icvReleaseCapture_FFMPEG_p = (CvReleaseCapture_Plugin)cvReleaseCapture_FFMPEG;
139 icvGrabFrame_FFMPEG_p = (CvGrabFrame_Plugin)cvGrabFrame_FFMPEG;
140 icvRetrieveFrame_FFMPEG_p = (CvRetrieveFrame_Plugin)cvRetrieveFrame_FFMPEG;
141 icvSetCaptureProperty_FFMPEG_p = (CvSetCaptureProperty_Plugin)cvSetCaptureProperty_FFMPEG;
142 icvGetCaptureProperty_FFMPEG_p = (CvGetCaptureProperty_Plugin)cvGetCaptureProperty_FFMPEG;
143 icvCreateVideoWriter_FFMPEG_p = (CvCreateVideoWriter_Plugin)cvCreateVideoWriter_FFMPEG;
144 icvReleaseVideoWriter_FFMPEG_p = (CvReleaseVideoWriter_Plugin)cvReleaseVideoWriter_FFMPEG;
145 icvWriteFrame_FFMPEG_p = (CvWriteFrame_Plugin)cvWriteFrame_FFMPEG;
151 class CvCapture_FFMPEG_proxy :
155 CvCapture_FFMPEG_proxy() { ffmpegCapture = 0; }
156 virtual ~CvCapture_FFMPEG_proxy() { close(); }
158 virtual double getProperty(int propId)
160 return ffmpegCapture ? icvGetCaptureProperty_FFMPEG_p(ffmpegCapture, propId) : 0;
162 virtual bool setProperty(int propId, double value)
164 return ffmpegCapture ? icvSetCaptureProperty_FFMPEG_p(ffmpegCapture, propId, value)!=0 : false;
166 virtual bool grabFrame()
168 return ffmpegCapture ? icvGrabFrame_FFMPEG_p(ffmpegCapture)!=0 : false;
170 virtual IplImage* retrieveFrame(int)
172 unsigned char* data = 0;
173 int step=0, width=0, height=0, cn=0;
175 if (!ffmpegCapture ||
176 !icvRetrieveFrame_FFMPEG_p(ffmpegCapture, &data, &step, &width, &height, &cn))
178 cvInitImageHeader(&frame, cvSize(width, height), 8, cn);
179 cvSetData(&frame, data, step);
182 virtual bool open( const char* filename )
184 icvInitFFMPEG::Init();
187 if( !icvCreateFileCapture_FFMPEG_p )
189 ffmpegCapture = icvCreateFileCapture_FFMPEG_p( filename );
190 return ffmpegCapture != 0;
194 if( ffmpegCapture && icvReleaseCapture_FFMPEG_p )
195 icvReleaseCapture_FFMPEG_p( &ffmpegCapture );
196 assert( ffmpegCapture == 0 );
206 CvCapture* cvCreateFileCapture_FFMPEG_proxy(const char * filename)
208 CvCapture_FFMPEG_proxy* result = new CvCapture_FFMPEG_proxy;
209 if( result->open( filename ))
215 class CvVideoWriter_FFMPEG_proxy :
219 CvVideoWriter_FFMPEG_proxy() { ffmpegWriter = 0; }
220 virtual ~CvVideoWriter_FFMPEG_proxy() { close(); }
222 virtual bool writeFrame( const IplImage* image )
226 CV_Assert(image->depth == 8);
228 return icvWriteFrame_FFMPEG_p(ffmpegWriter, (const uchar*)image->imageData,
229 image->widthStep, image->width, image->height, image->nChannels, image->origin) !=0;
231 virtual bool open( const char* filename, int fourcc, double fps, CvSize frameSize, bool isColor )
233 icvInitFFMPEG::Init();
235 if( !icvCreateVideoWriter_FFMPEG_p )
237 ffmpegWriter = icvCreateVideoWriter_FFMPEG_p( filename, fourcc, fps, frameSize.width, frameSize.height, isColor );
238 return ffmpegWriter != 0;
243 if( ffmpegWriter && icvReleaseVideoWriter_FFMPEG_p )
244 icvReleaseVideoWriter_FFMPEG_p( &ffmpegWriter );
245 assert( ffmpegWriter == 0 );
254 CvVideoWriter* cvCreateVideoWriter_FFMPEG_proxy( const char* filename, int fourcc,
255 double fps, CvSize frameSize, int isColor )
257 CvVideoWriter_FFMPEG_proxy* result = new CvVideoWriter_FFMPEG_proxy;
259 if( result->open( filename, fourcc, fps, frameSize, isColor != 0 ))