home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / plugins.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  7.3 KB  |  298 lines

  1. /* 
  2.  *  Plugins.cpp
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include <windows.h>
  25. #include <stdio.h>
  26. #include "plugins.h"
  27. #include "FormatDefs.h"
  28. #include "auxiliary.h"
  29.  
  30.  
  31. char load_directory[MAX_PATH];
  32.  
  33.  
  34.  
  35.  
  36. int FlPluginWrapper::SetSettings( ui8 *settings, ui32 settings_size )
  37. {
  38.   cfgblockinfo_s cfg;
  39.   
  40.   // Store whatever settings you may have
  41.   if( settings_size && settings )
  42.   {
  43.     cfg.buf = settings;
  44.     cfg.bufsize = settings_size;
  45.     
  46.     floutentry( flo_setinfo, flo_setblockinfo, (ui32)&cfg );
  47.   }
  48.   return settings_size;
  49. }
  50.  
  51. int FlPluginWrapper::GetSettings( ui8 **settings, ui32 *settings_size )
  52. {
  53.   cfgblockinfo_s cfg;
  54.   // Get Whatever settings you may have
  55.   floutentry( flo_getinfo, flo_getblockinfo, (ui32)&cfg );
  56.   if( cfg.buf && cfg.bufsize) {
  57.     *settings = cfg.buf;
  58.     *settings_size = cfg.bufsize;
  59.   }
  60.   else {
  61.     *settings_size = 0;
  62.   }
  63.   return cfg.bufsize;
  64. }
  65.  
  66.  
  67. int SaveSettings(PBYTE settings, int settings_size, char *name){
  68.   char szTemp[1024];
  69.   char validname[1024];
  70.   FILE *file;
  71.   
  72.   strcpy( validname, name );
  73.   validateFileName( validname );
  74.   
  75.   strcpy(szTemp, load_directory);
  76.   strcat(szTemp,"\\Settings" );
  77.   //Creating settings folder
  78.   CreateDirectory(szTemp, NULL);
  79.   
  80.   sprintf(szTemp, "%s\\%s.settings.flask", szTemp,validname);
  81.   if(!(file=fopen(szTemp, "wb"))){
  82.     //Ooops
  83.     return 0;
  84.   }
  85.   else
  86.   {
  87.     int version = FLOUT_VER;
  88.     fwrite( &version, 1, sizeof(int), file);
  89.     fwrite( &settings_size, 1, sizeof (int), file);
  90.     fwrite( settings, settings_size, 1, file);
  91.     fclose(file);
  92.     return 1;
  93.   }
  94. }
  95.  
  96. int FlushPluginSettings( TPlugins *plugs ){
  97.   int i;
  98.   ui8 *settings;
  99.   ui32 settings_size;
  100.  
  101.   for(i=0; i<plugs->OutPluginCount; i++)
  102.   {
  103.     FlPluginWrapper *pw = &plugs->outPlugs[i].plug;
  104.  
  105.     if( pw->GetSettings( &settings, &settings_size ) )
  106.       SaveSettings(settings, settings_size, plugs->outPlugs[i].ID);
  107.   }
  108.   return 0;
  109. }
  110.  
  111. static int RetrieveSettings( TPlugins *plugs )
  112. {
  113.     char szTemp[1024], validname[1024];
  114.     FILE *file;
  115.   int settings_size, version, i;
  116.   ui8 *settings;
  117.  
  118.   for(i=0; i<plugs->OutPluginCount; i++)
  119.   {
  120.     FlPluginWrapper *pw = &plugs->outPlugs[i].plug;
  121.  
  122.     strcpy( validname, plugs->outPlugs[i].ID );
  123.     validateFileName( validname );
  124.  
  125.       sprintf(szTemp, "%s\\Settings\\%s.settings.flask", load_directory, validname );
  126.  
  127.     if( file = fopen(szTemp, "rb") ) {
  128.  
  129.       fread( &version, 1, sizeof (int), file);
  130.  
  131.       if( version!=FLOUT_VER )
  132.         continue;
  133.  
  134.       fread( &settings_size, 1, sizeof (int), file );
  135.           settings = (unsigned char *)malloc(settings_size);
  136.           fread( settings, settings_size, 1, file);
  137.           fclose(file);
  138.       
  139.       pw->SetSettings( settings, settings_size );
  140.  
  141.       free( settings );
  142.       }
  143.   }
  144.     return 1;
  145. }
  146.  
  147. static int PluginStartup( TPlugins *plugs, char *filename ){
  148.  
  149.         HINSTANCE            hinstLib; 
  150.     int nCount;
  151.         int current = plugs->OutPluginCount;
  152.     int currentmod = plugs->OutModCount;
  153.         floutentry_ptr floutentry;
  154.     floutentry_getids_ptr floutentry_getids;
  155.  
  156.  
  157.  
  158.         hinstLib = LoadLibrary(filename); 
  159.         // If the handle is valid, try to get the function address.
  160.         if (!hinstLib)
  161.             return 0;
  162.  
  163.         plugs->outmod[currentmod].hInstLib = hinstLib;
  164.  
  165.     // Get the get ids function
  166.     floutentry_getids = (floutentry_getids_ptr) GetProcAddress(hinstLib, "floutentry_getids");
  167.     if(!floutentry_getids) {
  168.             FreeLibrary(hinstLib);
  169.             plugs->outmod[currentmod].hInstLib = NULL;
  170.             return 0;
  171.         }
  172.  
  173.     // Get the entry
  174.         floutentry = (floutentry_ptr) GetProcAddress(hinstLib, "floutentry");
  175.         if(!floutentry){
  176.             FreeLibrary(hinstLib);
  177.             plugs->outmod[currentmod].hInstLib = NULL;
  178.             return 0;
  179.         }
  180.  
  181.     // Get number of plugins available in this library
  182.     flohandle_t *pHandles;
  183.     nCount = floutentry_getids( &pHandles );
  184.     if(!nCount) {
  185.             FreeLibrary(hinstLib);
  186.             plugs->outmod[currentmod].hInstLib = NULL;
  187.             return 0;
  188.         }
  189.  
  190.     for( int i=0; i<nCount; i++ )
  191.     {
  192.       startup_s st;
  193.  
  194.       plugs->outPlugs[current].plug.Set( pHandles[i], floutentry );
  195.  
  196.       if( floutentry( pHandles[i], flo_startup, (ui32)&st, 0)!=flo_ok){
  197.                // Free the DLL module.
  198.               FreeLibrary(hinstLib);
  199.               plugs->outmod[currentmod].hInstLib = NULL;
  200.               return 0;
  201.           }
  202.  
  203.       plugs->outPlugs[current].plug.SetStartup( &st );
  204.       plugs->outPlugs[current].fileType   = st.moduleid;
  205.           strcpy( plugs->outPlugs[current].ID , st.modulename );
  206.  
  207.  
  208.       // Supported formats
  209.       videoinfo_s vi;
  210.  
  211.       floutentry( pHandles[i], flo_getinfo, flo_getvideoinfo, (ui32)&vi );
  212.  
  213.       long nSupported=0;
  214.  
  215.       nSupported |= vi.supported_fmt & FLO_VIDEO_RGB32 ? FRAME_RGB32 : 0;
  216.       nSupported |= vi.supported_fmt & FLO_VIDEO_YUY2  ? FRAME_YUY2  : 0;
  217.       nSupported |= vi.supported_fmt & FLO_VIDEO_YV12  ? FRAME_YV12  : 0;
  218.  
  219.       plugs->outPlugs[current].nSupportedFormats = nSupported;
  220.  
  221.       plugs->OutPluginCount++;
  222.       current++;
  223.     }
  224.  
  225.     plugs->OutModCount++;
  226.  
  227.         return plugs->OutPluginCount;
  228. }
  229.  
  230. int LoadPlugins( TPlugins *plugs, char *program_directory )
  231. {
  232.     WIN32_FIND_DATA find_data;
  233.     char            directory[MAX_PATH];
  234.     HANDLE          search_handle;
  235.     int i;
  236.     int loaded_ok=0;
  237.  
  238.     if(!program_directory)
  239.         return 0;
  240.  
  241.   plugs->OutPluginCount = 0;
  242.   plugs->OutModCount = 0;
  243.  
  244.     // StartUp stuff
  245.     for(i=0; i<MAX_OUT_PLUGINS; i++)
  246.     {
  247.         plugs->outmod[i].hInstLib = NULL;
  248.     }
  249.     
  250.     // try flaskmpegpeasopeich.dll for compatibility
  251.     if(PluginStartup( plugs,  "flaskmpegpeasopeich.dll" ))
  252.             loaded_ok++;
  253.  
  254.     strcpy( load_directory, program_directory);    
  255.     sprintf(directory, "%s\\*.cm.flask", load_directory);
  256.  
  257.     i=0;
  258.     search_handle = FindFirstFile(directory, &find_data);
  259.     if(search_handle==INVALID_HANDLE_VALUE){
  260.         return loaded_ok;
  261.     }
  262.     else{
  263.         if(PluginStartup( plugs,  find_data.cFileName ))
  264.             loaded_ok++;
  265.  
  266.         while( FindNextFile(search_handle, &find_data ) ){
  267.             if(PluginStartup( plugs,  find_data.cFileName ))
  268.                 loaded_ok++;
  269.         }
  270.         FindClose(search_handle);
  271.     }
  272.  
  273.   // Finally load the settings
  274.   RetrieveSettings( plugs );
  275.     return loaded_ok;
  276. }
  277.  
  278. int ClosePlugins(TPlugins *plugs)
  279. {
  280.     int i;
  281.  
  282.   FlushPluginSettings(plugs);
  283.  
  284.   // ShutDown first
  285.     for(i=0; i<plugs->OutPluginCount; i++)
  286.     plugs->outPlugs[i].plug.floutentry(flo_shutdown ,0,0);
  287.  
  288.  
  289.   for(i=0; i<plugs->OutModCount; i++)
  290.     {
  291.     if( plugs->outPlugs[i].hinstLib )
  292.     {
  293.           FreeLibrary(plugs->outPlugs[i].hinstLib);
  294.       plugs->outPlugs[i].hinstLib = 0;
  295.     }
  296.     }
  297.     return 0;
  298. }