Move the sources to trunk
[opencv] / otherlibs / highgui / cvcap_w32.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 "_highgui.h"
43
44 static int ffmpeg_initialized = 0;
45 static CvCaptureFromFile icvCaptureFromFile_FFMPEG_p = 0;
46 static CvCreateVideoWriter icvCreateVideoWriter_FFMPEG_p = 0;
47 static CvWriteFrame icvWriteFrame_FFMPEG_p = 0;
48 static CvReleaseVideoWriter icvReleaseVideoWriter_FFMPEG_p = 0;
49 static HMODULE ffopencv = 0;
50
51 static void
52 icvInitFFMPEG(void)
53 {
54     if( !ffmpeg_initialized )
55     {
56 #ifdef _DEBUG
57 #define ffopencv_suffix_dbg "d"
58 #else
59 #define ffopencv_suffix_dbg ""
60 #endif
61 #if defined EM64T
62 #define ffopencv_suffix "_64"
63 #else
64 #define ffopencv_suffix ""
65 #endif
66
67 #define ffopencv_name_m2(a,b,c) "ffopencv" #a #b #c ffopencv_suffix ffopencv_suffix_dbg ".dll"
68 #define ffopencv_name_m(a,b,c) ffopencv_name_m2(a,b,c)
69         const char* ffopencv_name =
70             ffopencv_name_m(CV_MAJOR_VERSION,CV_MINOR_VERSION,CV_SUBMINOR_VERSION);
71
72         ffopencv = LoadLibrary( ffopencv_name );
73         if( ffopencv )
74         {
75             icvCaptureFromFile_FFMPEG_p =
76                 (CvCaptureFromFile)GetProcAddress(ffopencv, "cvCaptureFromFile_FFMPEG");
77             icvCreateVideoWriter_FFMPEG_p =
78                 (CvCreateVideoWriter)GetProcAddress(ffopencv, "cvCreateVideoWriter_FFMPEG");
79             icvWriteFrame_FFMPEG_p =
80                 (CvWriteFrame)GetProcAddress(ffopencv, "cvWriteFrame_FFMPEG");
81             icvReleaseVideoWriter_FFMPEG_p =
82                 (CvReleaseVideoWriter)GetProcAddress(ffopencv, "cvReleaseVideoWriter_FFMPEG");
83         }
84
85         ffmpeg_initialized = 1;
86     }
87 }
88
89 CvCapture * cvCaptureFromFile_Win32 (const char * filename)
90 {
91     CvCapture* result = 0;
92     
93     icvInitFFMPEG();
94
95     if( icvCaptureFromFile_FFMPEG_p )
96         result = icvCaptureFromFile_FFMPEG_p(filename);
97
98     if( !result )
99         result = cvCaptureFromFile_VFW(filename);
100
101     return result;
102 }
103
104
105 CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
106                                             double fps, CvSize frameSize, int is_color )
107 {
108     CvVideoWriter* result = 0;
109     
110     icvInitFFMPEG();
111
112     // as video writer does not have virtual functions, we should be
113     // careful and do not mix FFMPEG & VFW. Therefore, use only one of the interfaces
114     if( icvCreateVideoWriter_FFMPEG_p )
115         result = icvCreateVideoWriter_FFMPEG_p(filename, fourcc, fps, frameSize, is_color);
116     else
117         result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
118
119     return result;
120 }
121
122 CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
123 {
124     int result;
125     
126     if( icvCreateVideoWriter_FFMPEG_p && icvWriteFrame_FFMPEG_p )
127         result = icvWriteFrame_FFMPEG_p( writer, image );
128     else
129         result = cvWriteFrame_VFW( writer, image );
130
131     return result;
132 }
133
134 CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** writer )
135 {
136     if( icvCreateVideoWriter_FFMPEG_p && icvReleaseVideoWriter_FFMPEG_p )
137         icvReleaseVideoWriter_FFMPEG_p( writer );
138     else
139         cvReleaseVideoWriter_VFW( writer );
140 }