CLAHE Python bindings
[profile/ivi/opencv.git] / modules / highgui / src / cap_ffmpeg.cpp
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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 #ifdef HAVE_FFMPEG
45 #include "cap_ffmpeg_impl.hpp"
46 #else
47 #include "cap_ffmpeg_api.hpp"
48 #endif
49
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;
59
60 static cv::Mutex _icvInitFFMPEG_mutex;
61
62 class icvInitFFMPEG
63 {
64 public:
65     static void Init()
66     {
67         cv::AutoLock al(_icvInitFFMPEG_mutex);
68         static icvInitFFMPEG init;
69     }
70
71 private:
72     #if defined WIN32 || defined _WIN32
73     HMODULE icvFFOpenCV;
74
75     ~icvInitFFMPEG()
76     {
77         if (icvFFOpenCV)
78         {
79             FreeLibrary(icvFFOpenCV);
80             icvFFOpenCV = 0;
81         }
82     }
83     #endif
84
85     icvInitFFMPEG()
86     {
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__)
91             "_64"
92         #endif
93             ".dll";
94
95         icvFFOpenCV = LoadLibrary( module_name );
96         if( icvFFOpenCV )
97         {
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");
116
117 #if 0
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 )
127             {
128                 printf("Successfully initialized ffmpeg plugin!\n");
129             }
130             else
131             {
132                 printf("Failed to load FFMPEG plugin: module handle=%p\n", icvFFOpenCV);
133             }
134 #endif
135         }
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;
146     #endif
147     }
148 };
149
150
151 class CvCapture_FFMPEG_proxy : 
152         public CvCapture
153 {
154 public:
155     CvCapture_FFMPEG_proxy() { ffmpegCapture = 0; }
156     virtual ~CvCapture_FFMPEG_proxy() { close(); }
157
158     virtual double getProperty(int propId)
159     {
160         return ffmpegCapture ? icvGetCaptureProperty_FFMPEG_p(ffmpegCapture, propId) : 0;
161     }
162     virtual bool setProperty(int propId, double value)
163     {
164         return ffmpegCapture ? icvSetCaptureProperty_FFMPEG_p(ffmpegCapture, propId, value)!=0 : false;
165     }
166     virtual bool grabFrame()
167     {
168         return ffmpegCapture ? icvGrabFrame_FFMPEG_p(ffmpegCapture)!=0 : false;
169     }
170     virtual IplImage* retrieveFrame(int)
171     {
172         unsigned char* data = 0;
173         int step=0, width=0, height=0, cn=0;
174
175         if (!ffmpegCapture ||
176            !icvRetrieveFrame_FFMPEG_p(ffmpegCapture, &data, &step, &width, &height, &cn))
177                         return 0;
178         cvInitImageHeader(&frame, cvSize(width, height), 8, cn);
179         cvSetData(&frame, data, step);
180         return &frame;
181     }
182     virtual bool open( const char* filename )
183     {
184         icvInitFFMPEG::Init();
185         close();
186
187         if( !icvCreateFileCapture_FFMPEG_p )
188             return false;
189         ffmpegCapture = icvCreateFileCapture_FFMPEG_p( filename );
190         return ffmpegCapture != 0;
191     }
192     virtual void close()
193     {
194         if( ffmpegCapture && icvReleaseCapture_FFMPEG_p )
195             icvReleaseCapture_FFMPEG_p( &ffmpegCapture );
196         assert( ffmpegCapture == 0 );
197         ffmpegCapture = 0;
198     }
199
200 protected:
201     void* ffmpegCapture;
202     IplImage frame;
203 };
204
205
206 CvCapture* cvCreateFileCapture_FFMPEG_proxy(const char * filename)
207 {
208     CvCapture_FFMPEG_proxy* result = new CvCapture_FFMPEG_proxy;
209     if( result->open( filename ))
210         return result;
211     delete result;
212 #ifdef HAVE_VFW
213     return cvCreateFileCapture_VFW(filename);
214 #else
215     return 0;
216 #endif
217 }
218
219 class CvVideoWriter_FFMPEG_proxy : 
220         public CvVideoWriter
221 {
222 public:
223     CvVideoWriter_FFMPEG_proxy() { ffmpegWriter = 0; }
224     virtual ~CvVideoWriter_FFMPEG_proxy() { close(); }
225
226     virtual bool writeFrame( const IplImage* image )
227     {
228         if(!ffmpegWriter)
229             return false;
230         CV_Assert(image->depth == 8);
231
232         return icvWriteFrame_FFMPEG_p(ffmpegWriter, (const uchar*)image->imageData,
233              image->widthStep, image->width, image->height, image->nChannels, image->origin) !=0;
234     }
235     virtual bool open( const char* filename, int fourcc, double fps, CvSize frameSize, bool isColor )
236     {
237         icvInitFFMPEG::Init();
238         close();
239         if( !icvCreateVideoWriter_FFMPEG_p )
240             return false;
241         ffmpegWriter = icvCreateVideoWriter_FFMPEG_p( filename, fourcc, fps, frameSize.width, frameSize.height, isColor );
242         return ffmpegWriter != 0;
243     }
244
245     virtual void close()
246     {
247         if( ffmpegWriter && icvReleaseVideoWriter_FFMPEG_p )
248             icvReleaseVideoWriter_FFMPEG_p( &ffmpegWriter );
249         assert( ffmpegWriter == 0 );
250         ffmpegWriter = 0;
251     }
252
253 protected:
254     void* ffmpegWriter;
255 };
256
257
258 CvVideoWriter* cvCreateVideoWriter_FFMPEG_proxy( const char* filename, int fourcc,
259                                           double fps, CvSize frameSize, int isColor )
260 {
261     CvVideoWriter_FFMPEG_proxy* result = new CvVideoWriter_FFMPEG_proxy;
262
263     if( result->open( filename, fourcc, fps, frameSize, isColor != 0 ))
264         return result;
265     delete result;
266 #ifdef HAVE_VFW
267      return cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, isColor);
268  #else
269     return 0;
270 #endif
271 }