Home | History | Annotate | Download | only in src
      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 #if defined HAVE_FFMPEG && !defined WIN32
     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     # ifdef WINRT
     89         const wchar_t* module_name = L"opencv_ffmpeg"
     90             CVAUX_STRW(CV_MAJOR_VERSION) CVAUX_STRW(CV_MINOR_VERSION) CVAUX_STRW(CV_SUBMINOR_VERSION)
     91         #if (defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__)
     92             L"_64"
     93         #endif
     94             L".dll";
     95 
     96         icvFFOpenCV = LoadPackagedLibrary( module_name, 0 );
     97     # else
     98         const char* module_name = "opencv_ffmpeg"
     99             CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
    100         #if (defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__)
    101             "_64"
    102         #endif
    103             ".dll";
    104 
    105         icvFFOpenCV = LoadLibrary( module_name );
    106     # endif
    107 
    108         if( icvFFOpenCV )
    109         {
    110             icvCreateFileCapture_FFMPEG_p =
    111                 (CvCreateFileCapture_Plugin)GetProcAddress(icvFFOpenCV, "cvCreateFileCapture_FFMPEG");
    112             icvReleaseCapture_FFMPEG_p =
    113                 (CvReleaseCapture_Plugin)GetProcAddress(icvFFOpenCV, "cvReleaseCapture_FFMPEG");
    114             icvGrabFrame_FFMPEG_p =
    115                 (CvGrabFrame_Plugin)GetProcAddress(icvFFOpenCV, "cvGrabFrame_FFMPEG");
    116             icvRetrieveFrame_FFMPEG_p =
    117                 (CvRetrieveFrame_Plugin)GetProcAddress(icvFFOpenCV, "cvRetrieveFrame_FFMPEG");
    118             icvSetCaptureProperty_FFMPEG_p =
    119                 (CvSetCaptureProperty_Plugin)GetProcAddress(icvFFOpenCV, "cvSetCaptureProperty_FFMPEG");
    120             icvGetCaptureProperty_FFMPEG_p =
    121                 (CvGetCaptureProperty_Plugin)GetProcAddress(icvFFOpenCV, "cvGetCaptureProperty_FFMPEG");
    122             icvCreateVideoWriter_FFMPEG_p =
    123                 (CvCreateVideoWriter_Plugin)GetProcAddress(icvFFOpenCV, "cvCreateVideoWriter_FFMPEG");
    124             icvReleaseVideoWriter_FFMPEG_p =
    125                 (CvReleaseVideoWriter_Plugin)GetProcAddress(icvFFOpenCV, "cvReleaseVideoWriter_FFMPEG");
    126             icvWriteFrame_FFMPEG_p =
    127                 (CvWriteFrame_Plugin)GetProcAddress(icvFFOpenCV, "cvWriteFrame_FFMPEG");
    128 
    129 #if 0
    130             if( icvCreateFileCapture_FFMPEG_p != 0 &&
    131                 icvReleaseCapture_FFMPEG_p != 0 &&
    132                 icvGrabFrame_FFMPEG_p != 0 &&
    133                 icvRetrieveFrame_FFMPEG_p != 0 &&
    134                 icvSetCaptureProperty_FFMPEG_p != 0 &&
    135                 icvGetCaptureProperty_FFMPEG_p != 0 &&
    136                 icvCreateVideoWriter_FFMPEG_p != 0 &&
    137                 icvReleaseVideoWriter_FFMPEG_p != 0 &&
    138                 icvWriteFrame_FFMPEG_p != 0 )
    139             {
    140                 printf("Successfully initialized ffmpeg plugin!\n");
    141             }
    142             else
    143             {
    144                 printf("Failed to load FFMPEG plugin: module handle=%p\n", icvFFOpenCV);
    145             }
    146 #endif
    147         }
    148     #elif defined HAVE_FFMPEG
    149         icvCreateFileCapture_FFMPEG_p = (CvCreateFileCapture_Plugin)cvCreateFileCapture_FFMPEG;
    150         icvReleaseCapture_FFMPEG_p = (CvReleaseCapture_Plugin)cvReleaseCapture_FFMPEG;
    151         icvGrabFrame_FFMPEG_p = (CvGrabFrame_Plugin)cvGrabFrame_FFMPEG;
    152         icvRetrieveFrame_FFMPEG_p = (CvRetrieveFrame_Plugin)cvRetrieveFrame_FFMPEG;
    153         icvSetCaptureProperty_FFMPEG_p = (CvSetCaptureProperty_Plugin)cvSetCaptureProperty_FFMPEG;
    154         icvGetCaptureProperty_FFMPEG_p = (CvGetCaptureProperty_Plugin)cvGetCaptureProperty_FFMPEG;
    155         icvCreateVideoWriter_FFMPEG_p = (CvCreateVideoWriter_Plugin)cvCreateVideoWriter_FFMPEG;
    156         icvReleaseVideoWriter_FFMPEG_p = (CvReleaseVideoWriter_Plugin)cvReleaseVideoWriter_FFMPEG;
    157         icvWriteFrame_FFMPEG_p = (CvWriteFrame_Plugin)cvWriteFrame_FFMPEG;
    158     #endif
    159     }
    160 };
    161 
    162 
    163 class CvCapture_FFMPEG_proxy :
    164     public CvCapture
    165 {
    166 public:
    167     CvCapture_FFMPEG_proxy() { ffmpegCapture = 0; }
    168     virtual ~CvCapture_FFMPEG_proxy() { close(); }
    169 
    170     virtual double getProperty(int propId) const
    171     {
    172         return ffmpegCapture ? icvGetCaptureProperty_FFMPEG_p(ffmpegCapture, propId) : 0;
    173     }
    174     virtual bool setProperty(int propId, double value)
    175     {
    176         return ffmpegCapture ? icvSetCaptureProperty_FFMPEG_p(ffmpegCapture, propId, value)!=0 : false;
    177     }
    178     virtual bool grabFrame()
    179     {
    180         return ffmpegCapture ? icvGrabFrame_FFMPEG_p(ffmpegCapture)!=0 : false;
    181     }
    182     virtual IplImage* retrieveFrame(int)
    183     {
    184         unsigned char* data = 0;
    185         int step=0, width=0, height=0, cn=0;
    186 
    187         if (!ffmpegCapture ||
    188            !icvRetrieveFrame_FFMPEG_p(ffmpegCapture, &data, &step, &width, &height, &cn))
    189             return 0;
    190         cvInitImageHeader(&frame, cvSize(width, height), 8, cn);
    191         cvSetData(&frame, data, step);
    192         return &frame;
    193     }
    194     virtual bool open( const char* filename )
    195     {
    196         icvInitFFMPEG::Init();
    197         close();
    198 
    199         if( !icvCreateFileCapture_FFMPEG_p )
    200             return false;
    201         ffmpegCapture = icvCreateFileCapture_FFMPEG_p( filename );
    202         return ffmpegCapture != 0;
    203     }
    204     virtual void close()
    205     {
    206         if( ffmpegCapture && icvReleaseCapture_FFMPEG_p )
    207             icvReleaseCapture_FFMPEG_p( &ffmpegCapture );
    208         assert( ffmpegCapture == 0 );
    209         ffmpegCapture = 0;
    210     }
    211 
    212 protected:
    213     void* ffmpegCapture;
    214     IplImage frame;
    215 };
    216 
    217 
    218 CvCapture* cvCreateFileCapture_FFMPEG_proxy(const char * filename)
    219 {
    220     CvCapture_FFMPEG_proxy* result = new CvCapture_FFMPEG_proxy;
    221     if( result->open( filename ))
    222         return result;
    223     delete result;
    224     return 0;
    225 }
    226 
    227 class CvVideoWriter_FFMPEG_proxy :
    228     public CvVideoWriter
    229 {
    230 public:
    231     CvVideoWriter_FFMPEG_proxy() { ffmpegWriter = 0; }
    232     virtual ~CvVideoWriter_FFMPEG_proxy() { close(); }
    233 
    234     virtual bool writeFrame( const IplImage* image )
    235     {
    236         if(!ffmpegWriter)
    237             return false;
    238         CV_Assert(image->depth == 8);
    239 
    240         return icvWriteFrame_FFMPEG_p(ffmpegWriter, (const uchar*)image->imageData,
    241              image->widthStep, image->width, image->height, image->nChannels, image->origin) !=0;
    242     }
    243     virtual bool open( const char* filename, int fourcc, double fps, CvSize frameSize, bool isColor )
    244     {
    245         icvInitFFMPEG::Init();
    246         close();
    247         if( !icvCreateVideoWriter_FFMPEG_p )
    248             return false;
    249         ffmpegWriter = icvCreateVideoWriter_FFMPEG_p( filename, fourcc, fps, frameSize.width, frameSize.height, isColor );
    250         return ffmpegWriter != 0;
    251     }
    252 
    253     virtual void close()
    254     {
    255         if( ffmpegWriter && icvReleaseVideoWriter_FFMPEG_p )
    256             icvReleaseVideoWriter_FFMPEG_p( &ffmpegWriter );
    257         assert( ffmpegWriter == 0 );
    258         ffmpegWriter = 0;
    259     }
    260 
    261 protected:
    262     void* ffmpegWriter;
    263 };
    264 
    265 
    266 CvVideoWriter* cvCreateVideoWriter_FFMPEG_proxy( const char* filename, int fourcc,
    267                                           double fps, CvSize frameSize, int isColor )
    268 {
    269     CvVideoWriter_FFMPEG_proxy* result = new CvVideoWriter_FFMPEG_proxy;
    270 
    271     if( result->open( filename, fourcc, fps, frameSize, isColor != 0 ))
    272         return result;
    273     delete result;
    274     return 0;
    275 }
    276