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

  1. /* 
  2.  *  FlasKMPEG.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.  
  25.  
  26.  
  27. #include "FlasKMPEG.h"
  28.  
  29. #include "./Demux/Demux.h"
  30. #include "./Video/VideoWrapper.h"
  31. #include "./Audio/Audio.h"
  32. #include "./plugins.h"
  33. #include "./error.h"
  34. #include "./RunState.h"
  35. #include "./language.h"
  36. #include "./Misc/Graph.h"
  37. #include "./Misc/StatsStrings.h"
  38. #include "./FileOpen.h"
  39. #include "./AudioProperties.h"
  40. #include "./Audio/ExportAudio.h"
  41. #include "./Player.h"
  42. #include "./AudioProperties.h"
  43. #include "./ProgressDlg.h"
  44. #include "./Mism.h"
  45. #include "./Background.h"
  46. #include "version.h"
  47.  
  48.  
  49. #include <malloc.h>
  50. #include <commdlg.h>
  51. #include <CommCtrl.h> 
  52. #include "resource.h"
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <ctype.h>
  56. #include <fcntl.h>
  57. #include <math.h>
  58. #include <mbstring.h>
  59.  
  60.  
  61. #include ".\Resizer\resizer.h"
  62. #include ".\postprocessing.h"
  63. #include "auxiliary.h"
  64.  
  65.  
  66. #define MAX_LOADSTRING 100
  67. #define TESTING_IFO_PARSER
  68. //#define TESTING_AUDIO_OUTPUT
  69. //#define TESTING_MULTIAUDIO
  70. // Global Variables:
  71. extern "C" BOOL FPU_enabled;
  72.  
  73. BOOL FPU_enabled;
  74. HINSTANCE            hInst;                                // current instance
  75. TCHAR                szTitle[MAX_LOADSTRING];                                // The title bar text
  76. TCHAR                szWindowClass[MAX_LOADSTRING];                                // The title bar text
  77. HANDLE                hThrd;
  78. DWORD                    IDThread, ENCIDThread; 
  79. HANDLE                hEncodingThread;
  80.  
  81.  
  82.  
  83. unsigned char       *newblock;
  84. char                program_directory[MAX_PATH];
  85.  
  86.  
  87. TRunState rs;
  88.  
  89. CFrame frBackground(NULL);
  90.  
  91. HWND                  hMainWnd;
  92. BITMAPINFOHEADER    DibHeader;
  93. BITMAPINFO            DibInfo;
  94. BOOL                playerStopped;
  95. BOOL                 abortCompile;        //Use this to abort the compile
  96. BOOL                 audioFinished;
  97. BOOL                compileAborted;     //Use this to kill progress dialog
  98. BOOL                decodingFinished;    //The decoding thread has finished
  99. //framerate sync variables
  100. i64 myClock;
  101. static i64 frameSpan;
  102. static double audioSamplesPerFrame;
  103.  
  104. bool   imageWaiting;
  105. bool   firstTime;
  106. bool   firstImageAlreadyDecoded=false;
  107. presInfo pInfo;
  108. // Compiling globals
  109. int        audioPrevFrame;
  110. bool    pauseCompile;
  111. bool    compileIsPaused;
  112.  
  113. TFileProgress        FileProgress;
  114.  
  115. static CBackground *g_pBackground = NULL;
  116.  
  117.  
  118. typedef enum{LOAD_LIBRARY,
  119.      ENCODE,
  120.     UNLOAD_LIBRARY}        EncEnum;
  121. EncEnum        sendEncodingThread;
  122.  
  123.  
  124. // 
  125. #ifdef TESTING_AUDIO_OUTPUT
  126.     FILE    *test_sound_file;
  127. #endif
  128.  
  129. // Foward declarations of functions included in this code module:
  130. ATOM                MyRegisterClass(HINSTANCE hInstance);
  131. BOOL                InitInstance(HINSTANCE, int);
  132. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  133. LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  134. LRESULT CALLBACK    OutputOptionsDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  135. LRESULT CALLBACK    ProgressDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  136.  
  137.                void SetTextMenu();
  138.  
  139.                 
  140.                
  141. int APIENTRY WinMain(HINSTANCE hInstance,
  142.                      HINSTANCE hPrevInstance,
  143.                      LPSTR     lpCmdLine,
  144.                      int       nCmdShow)
  145. {
  146.      // TODO: Place code here.
  147.     MSG msg;
  148.     HACCEL hAccelTable;
  149.  
  150.   // Create Basic FlasKMPEG objects
  151.   char szTemp[MAX_PATH];
  152.   GetModuleFileName(NULL, szTemp, MAX_PATH);
  153.   // Copy just the path
  154.   // We do that by replacing the last \ with end-of-string.
  155.   char *p,*pbs = 0;
  156.   for (p = szTemp; *p ; p++) if (*p == '\\') pbs = p;
  157.   if (pbs) *pbs = '\0';
  158.   strcpy(program_directory, szTemp);
  159.   //_mbsnbcpy( (unsigned char *)program_directory, (unsigned char *)szTemp, strlen(szTemp) - strlen("FlasKMPEG.exe") -1 );
  160.  
  161.   rs.lng = new CLanguage(program_directory);
  162.   if(rs.lng){
  163.     rs.lng->Start();
  164.   }
  165.  
  166.     rs.profiler = new CProfile(program_directory);
  167.  
  168.     // Initialize global strings
  169.     strcpy( szTitle, GS(APPTITLE) );
  170.   //    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  171.     LoadString(hInstance, IDC_FLASKMPG2MPG1, szWindowClass, MAX_LOADSTRING);
  172.     MyRegisterClass(hInstance);
  173.  
  174.     // Perform application initialization:
  175.     if (!InitInstance (hInstance, nCmdShow)) 
  176.     {
  177.         return FALSE;
  178.     }
  179.  
  180.     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_FLASKMPG2MPG1);
  181.  
  182.     // Main message loop:
  183.     while (GetMessage(&msg, NULL, 0, 0)) 
  184.     {
  185.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  186.         {
  187.             TranslateMessage(&msg);
  188.             DispatchMessage(&msg);
  189.         }
  190.     }
  191.  
  192.     return msg.wParam;
  193. }
  194.  
  195. // file=> file to get extension from
  196. // ext => string to put extension
  197. char *get_file_ext(char *file, char *ext){
  198.     if(!file || !ext)
  199.         return NULL;
  200.     int len = strlen( file );
  201.  
  202.     ext[0] = file[len-3];
  203.     ext[1] = file[len-2];
  204.     ext[2] = file[len-1];
  205.     ext[3] = 0;
  206.     return ext;
  207. }
  208.  
  209.  
  210. //
  211. //  FUNCTION: MyRegisterClass()
  212. //
  213. //  PURPOSE: Registers the window class.
  214. //
  215. //  COMMENTS:
  216. //
  217. //    This function and its usage is only necessary if you want this code
  218. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  219. //    function that was added to Windows 95. It is important to call this function
  220. //    so that the application will get 'well formed' small icons associated
  221. //    with it.
  222. //
  223. ATOM MyRegisterClass(HINSTANCE hInstance)
  224. {
  225.   WNDCLASSEX wcex;
  226.   
  227.   wcex.cbSize = sizeof(WNDCLASSEX); 
  228.   
  229.   wcex.style            = CS_HREDRAW | CS_VREDRAW;
  230.   wcex.lpfnWndProc    = (WNDPROC)WndProc;
  231.   wcex.cbClsExtra        = 0;
  232.   wcex.cbWndExtra        = 0;
  233.   wcex.hInstance        = hInstance;
  234.   wcex.hIcon            = (HICON)LoadImage(hInstance, (LPCTSTR)IDI_FLASKMPG2MPG1, IMAGE_ICON, 0,0,0);
  235.   wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  236.   wcex.hbrBackground    = 0; /*(HBRUSH) CreateSolidBrush(MASKCOLOR)*/;
  237.   wcex.lpszMenuName    = (LPCSTR)IDC_FLASKMPG2MPG1;
  238.   wcex.lpszClassName    = szWindowClass;
  239.   wcex.hIconSm        = NULL;//LoadIcon(wcex.hInstance, (LPCTSTR)IDI_FLASKMPG2MPG1);
  240.   
  241.   return RegisterClassEx(&wcex);
  242. }
  243.  
  244. //
  245. //   FUNCTION: InitInstance(HANDLE, int)
  246. //
  247. //   PURPOSE: Saves instance handle and creates main window
  248. //
  249. //   COMMENTS:
  250. //
  251. //        In this function, we save the instance handle in a global variable and
  252. //        create and display the main program window.
  253. //
  254. // Mesage handler for about box.
  255. LRESULT CALLBACK WelcomeDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  256. {
  257.   char szTemp[256];
  258.  
  259.   switch (message)
  260.   {
  261.         case WM_INITDIALOG:
  262.       // Change the title to current version
  263.       sprintf( szTemp, "Welcome to FlasKMPEG %d.%d.%d", FLASK_VERSION>>24, 
  264.                                                        (FLASK_VERSION>>16)&0xFF, 
  265.                                                        (FLASK_VERSION>>8)&0xFF );
  266.       DlgSetText( hDlg, IDC_TEXTO, szTemp );
  267.       return TRUE;
  268.       
  269.     case WM_COMMAND:
  270.       
  271.       
  272.       if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  273.       {
  274.         EndDialog(hDlg, LOWORD(wParam));
  275.         return TRUE;
  276.       }
  277.       break;
  278.   }
  279.   return FALSE;
  280. }
  281.  
  282. // WorkOut how many last files there are
  283. int LastFilesCount()
  284. {
  285.   int files=0;
  286.   for( int i=0; i<4; i++ )
  287.   {
  288.     if( rs.conf.lastFiles[i][0]!='\0' )
  289.       files++;
  290.   }
  291.   return files;
  292. }
  293.  
  294.  
  295. void InsertFileInLastFilesList( char *file )
  296. {
  297.   if(file)
  298.   {
  299.     // if the file is already there don't put it
  300.     for(int j=0; j<4; j++)
  301.       if( strcmp( rs.conf.lastFiles[j], file ) == 0 )
  302.         return;
  303.  
  304.     for(int i=3; i>=1; i--)
  305.     {
  306.       strcpy( rs.conf.lastFiles[i], rs.conf.lastFiles[i-1] );
  307.     }
  308.     strcpy( rs.conf.lastFiles[0], file );
  309.   }
  310. }
  311. // Rebuild the menu from all settings
  312. void SetTextMenu(){
  313.   HMENU menu;
  314.   int i;
  315.  
  316.   MenuSetText(GetMenu(hMainWnd), 0, GS(MENU_FILE));
  317.   MenuSetText(GetMenu(hMainWnd), 1, GS(MENU_PROFILES));
  318.   MenuSetText(GetMenu(hMainWnd), 2, GS(MENU_OPTIONS));
  319.   MenuSetText(GetMenu(hMainWnd), 3, GS(MENU_RUN));
  320.   MenuSetText(GetMenu(hMainWnd), 4, GS(MENU_ABOUT));
  321.   
  322.   // File preferences
  323.   menu = MenuGetPopup(GetMenu(hMainWnd), 0);
  324.   MenuSetText( menu, 0, GS(MENU_OPEN));
  325.     // LastFiles
  326.     while(RemoveMenu(menu, 2, MF_BYPOSITION ));
  327.     for(i=0; i<LastFilesCount(); i++)
  328.     {
  329.       MenuAddItem( menu, i+3, i+10500, rs.conf.lastFiles[i] );
  330.     }
  331.   MenuAddSeparator( menu, LastFilesCount() + 2 );
  332.   MenuAddItem( menu, LastFilesCount() + 3, IDM_EXIT, GS(MENU_EXIT) );
  333.   
  334.   // Presets
  335.   menu = MenuGetPopup(GetMenu(hMainWnd), 1);
  336.   while(RemoveMenu(menu, 0, MF_BYPOSITION ));
  337.   
  338.   for(i=0; i<rs.profiler->GetCount(); i++){
  339.     MenuAddItem( menu, i, i+10400, (rs.profiler->Get(i))->profile_name );
  340.   }
  341.   MenuCheck(hMainWnd, 10400+rs.profiler->GetSelectedIndex() );
  342.   
  343.   // Options preferences
  344.   menu = MenuGetPopup(GetMenu(hMainWnd), 2);
  345.   MenuSetText( menu, 0, GS(MENU_OUTPUTFORMAT));
  346.   MenuSetText( menu, 1, GS(MENU_OUTOPTIONS));
  347.   MenuSetText( menu, 3, GS(MENU_SELECTOUT));
  348.   MenuSetText( menu, 5, GS(MENU_LANGUAGE));
  349.   
  350.   // Output plugins
  351.   menu = MenuGetPopup(GetMenu(hMainWnd), 2);
  352.   menu = MenuGetPopup(menu, 3);
  353.   while(RemoveMenu(menu, 0, MF_BYPOSITION ));
  354.   
  355.   for(i=0; i<rs.plugs.OutPluginCount; i++){
  356.     MenuAddItem( menu, i, i+10300, rs.plugs.outPlugs[i].ID );
  357.   }
  358.   MenuCheck(hMainWnd, 10300+rs.selected_out_plug);
  359.   
  360.   //Languages menus
  361.         // Get languages popup
  362.   menu = MenuGetPopup(GetMenu(hMainWnd), 2);
  363.   menu = MenuGetPopup(menu, 5);
  364.   
  365.   while(RemoveMenu(menu, 0, MF_BYPOSITION ));
  366.   
  367.   
  368.   int current_lang = rs.lng->GetLanguage();
  369.   int n_lang = rs.lng->GetNumberLang();
  370.   for(i=0; i<n_lang; i++){
  371.     rs.lng->SetLanguage(i);
  372.     MenuAddItem(menu, i, i+10000, rs.lng->GetLanguageID() );
  373.   }
  374.   
  375.   rs.lng->SetLanguage(current_lang);
  376.   
  377.   //Run
  378.   menu = MenuGetPopup(GetMenu(hMainWnd), 3);
  379.   MenuSetText( menu, 0, GS(MENU_PLAYER));
  380.   // TODO language stuff for extract wav and audioplayer
  381.   MenuSetText( menu, 4, GS(MENU_STARTCONV));
  382.   
  383.   
  384.   DrawMenuBar( hMainWnd );
  385. }
  386.  
  387. void SetMenuMPEG(){
  388.     MenuCheck(hMainWnd, IDM_MPEG);
  389.     MenuUnCheck(hMainWnd, IDM_AVI);
  390.     MenuEnable(hMainWnd,IDM_MPEG1OPTIONS);
  391. }
  392. void SetMenuAVI(){
  393.     MenuCheck(hMainWnd, IDM_AVI);
  394.     MenuUnCheck(hMainWnd, IDM_MPEG);
  395.     //MenuDisable(hMainWnd,IDM_MPEG1OPTIONS);
  396.     MenuEnable(hMainWnd,IDM_MPEG1OPTIONS);
  397. }
  398. void EnableMenu(){
  399.   EnableMenuItem( GetMenu(hMainWnd), 1, MF_ENABLED|MF_BYPOSITION );
  400.   EnableMenuItem( GetMenu(hMainWnd), 2, MF_ENABLED|MF_BYPOSITION );
  401.   EnableMenuItem( GetMenu(hMainWnd), 3, MF_ENABLED|MF_BYPOSITION );
  402. }
  403. void DisableMenu(){
  404.   EnableMenuItem( GetMenu(hMainWnd), 1, MF_GRAYED|MF_BYPOSITION );
  405.   EnableMenuItem( GetMenu(hMainWnd), 2, MF_GRAYED|MF_BYPOSITION );
  406.   EnableMenuItem( GetMenu(hMainWnd), 3, MF_GRAYED|MF_BYPOSITION );
  407. }
  408.  
  409. // Disable/Enable AllMenu are called to 
  410. // disable/enable menu options when compiling
  411. void DisableAllMenu(){
  412.     DisableMenu();
  413.   EnableMenuItem( GetMenu(hMainWnd), 0, MF_GRAYED|MF_BYPOSITION );
  414.  
  415.   DragAcceptFiles(hMainWnd, FALSE);
  416.  
  417. }
  418. void EnableAllMenu(){
  419.     EnableMenu();
  420.   EnableMenuItem( GetMenu(hMainWnd), 0, MF_ENABLED|MF_BYPOSITION );
  421.  
  422.     DragAcceptFiles(hMainWnd, TRUE);
  423. }
  424.  
  425. void LoadDefaultProfile()
  426. {
  427.  
  428.  
  429.     rs.prof.crop            =    false;
  430.     rs.prof.cropTopOffset =    0;
  431.     rs.prof.cropHeight    =    288;
  432.     rs.prof.cropLeftOffset=    0;
  433.     rs.prof.cropWidth        =    352;
  434.  
  435.  
  436.   rs.prof.videocontrols = false;
  437.   rs.prof.brightness  = 128;
  438.   rs.prof.contrast   = 128;
  439.   rs.prof.hue  = 128;
  440.  
  441.     rs.prof.letterbox        =    false;
  442.     rs.prof.letterboxTop    =    0;
  443.     rs.prof.letterboxBottom=    0;
  444.     rs.prof.letterboxLeft    =    0;
  445.     rs.prof.letterboxRight=    0;
  446.  
  447.     rs.prof.deinterlace   =   0;
  448.     rs.prof.blend         =   1;
  449.     rs.prof.threshold     =  20;
  450.  
  451.     rs.prof.keepAspect                     = KEEP_AR | AR_11;
  452.     rs.prof.recons_progressive             = true;
  453.  
  454.   rs.prof.bResize = false;
  455.     rs.prof.InterpolatedWidth=352;
  456.     rs.prof.InterpolatedHeight=288;
  457.  
  458.     rs.prof.filter                         = FILTER_TABLEBICUBIC;
  459.  
  460.     rs.prof.timeBase.scale                 = 25;
  461.     rs.prof.timeBase.sampleSize            = 1;
  462.  
  463.   rs.prof.audioMode                      = DO_AUDIO;
  464.     rs.prof.outSampleFrequency             = 44100;
  465.     rs.prof.sampleFreqSameAsInput          = true;
  466.   rs.prof.sAudioProperties.drc           = false;
  467.   rs.prof.sAudioProperties.drc_value     = 3;
  468.   rs.prof.sAudioProperties.multichannel_volume = false;
  469.   rs.prof.sAudioProperties.center        = 0;
  470.   rs.prof.sAudioProperties.rear          = 0;
  471.   rs.prof.sAudioProperties.front         = 0;
  472.   rs.prof.sAudioProperties.normalize     = false;
  473.   rs.prof.sAudioProperties.normalize_value = 100;
  474.   rs.prof.sAudioProperties.dolby_surround_downmix = true;
  475.  
  476.  
  477.   strcpy(rs.prof.profile_name, "Default");
  478.  
  479.     return;
  480. }
  481. void LoadFirstTime(HINSTANCE hInstance, HWND hWnd){
  482.  
  483.   LoadDefaultProfile();
  484.  
  485.     rs.conf.version=FLASK_VERSION;
  486.     rs.conf.showDetailsInProgress=true;
  487.  
  488.   rs.conf.idctAutoSelect = 1;
  489.  
  490.   rs.conf.idctIndex=  0;
  491.   rs.conf.selectedTab=0;
  492.   
  493.   rs.conf.warn_overwrite=   true;
  494.   rs.conf.shutdown      =   false;
  495.   rs.conf.lurk_size     =   5000;
  496.   rs.conf.compileWhole       = true;
  497.   rs.conf.framesToCompile                = 3000;
  498.   strcpy(rs.conf.audioOutFile, "C:\\flaskAudioOut");
  499.   strcpy(rs.conf.outputFile, "C:\\flaskOut");
  500.   
  501.     rs.conf.priority      =   1;
  502.     rs.conf.displayVideo                   = true;
  503.  
  504.  
  505.     rs.conf.hOutputFile                    = NULL;
  506.     rs.conf.presentFrame                   = 0;
  507.     rs.conf.outputFormat                   = 0;
  508.   rs.conf.bProcessingFormatAuto          = true;
  509.   rs.conf.nProcessingFormat              = FRAME_YV12;
  510.   rs.conf.bDualPass                      = false;
  511.  
  512.   // Try to select odml avi plugin as default
  513.   for(int i=0; i<rs.plugs.OutPluginCount; i++)
  514.     if(rs.plugs.outPlugs[i].fileType == ODMLtype)
  515.     {
  516.       rs.conf.outputFormat = i;
  517.       break;
  518.     }
  519.  
  520.   rs.conf.selected_profile               = 0;
  521.  
  522.     rs.conf.firstTimeRunning= true;
  523.  
  524.   // Set no files as default
  525.   rs.conf.lastFiles[0][0] = '\0';
  526.   rs.conf.lastFiles[1][0] = '\0';
  527.   rs.conf.lastFiles[2][0] = '\0';
  528.   rs.conf.lastFiles[3][0] = '\0';
  529.  
  530.     SaveRegistryConfig(&rs.conf);
  531.     LoadRegistryConfig(&rs.conf);
  532.     DialogBox(hInstance, MAKEINTRESOURCE(IDD_WELCOME), hWnd, (DLGPROC)WelcomeDlg);
  533. }
  534.  
  535.  
  536. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  537. {
  538.    HWND hWnd;
  539.  
  540.    rs.hInst    = hInst    =  hInstance; // Store instance handle in our global variable
  541.    rs.hMainWnd = hMainWnd =  hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX ,
  542.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  543.  
  544.    if (!hWnd)
  545.    {
  546.       return FALSE;
  547.    }
  548.  
  549.    DragAcceptFiles(hWnd, TRUE);
  550.    InitCommonControls();
  551.  
  552.    // Check for MMX
  553.    if ( CPUGetSupportedExtensions()&CPU_SUPPORTS_MMX == false ) {
  554.       Critical("FlasKMPEG requires a processor with MMX to work\n");
  555.       return FALSE;
  556.    }
  557.  
  558.  
  559.     //Load plugins
  560.     if(!LoadPlugins(&rs.plugs, program_directory)){
  561.         PrintError(ERROR_PLUGIN, (int)hMainWnd, 0);
  562.         exit(0);
  563.     }
  564.  
  565.  
  566.  
  567.        //Starting options
  568.     if(LoadRegistryConfig(&rs.conf)==0)    //This is the first time. Load defaults
  569.     {
  570.         LoadFirstTime(hInstance, hWnd);
  571.     }
  572.     else
  573.     {
  574.         //Key existed. Check version
  575.         if(rs.conf.version!=FLASK_VERSION)
  576.             LoadFirstTime(hInstance, hWnd);
  577.         else
  578.             rs.conf.firstTimeRunning=false;
  579.     }
  580.  
  581.   // Loading Profiles
  582.   if(!rs.profiler->Refresh() )
  583.   {
  584.     // Oops. No profiles were present
  585.     LoadDefaultProfile();
  586.     // Try to add the default profile
  587.     rs.profiler->AddProfile( &rs.prof );
  588.     if( !rs.profiler->Refresh() )
  589.       return FALSE;
  590.   }
  591.   else
  592.   {
  593.     rs.profiler->Select( rs.conf.selected_profile );
  594.     rs.prof = *rs.profiler->GetSelected();
  595.   }
  596.   // If the first time running, select the default profile
  597.   if(rs.conf.firstTimeRunning)
  598.   {
  599.     rs.profiler->SelectDefault();
  600.     rs.prof = *rs.profiler->GetSelected();
  601.   }
  602.  
  603.  
  604.   // Load Misms. Needs a profile to start with.
  605.   if(!StartMism()){
  606.     exit(0);
  607.   }
  608.     if(rs.plugs.OutPluginCount <= rs.conf.outputFormat)
  609.         rs.conf.outputFormat = 0;
  610.  
  611.     rs.selected_out_plug = rs.conf.outputFormat;
  612.     SetTextMenu();
  613.     //Miscelaneous Inits
  614.     WindowClientResize( hMainWnd, 352, 288);
  615.   // Move window
  616.   WindowMove( hWnd, 20, 20);
  617.     compileAborted=true;
  618.     playerStopped=true;
  619.     rs.startFilePos=0;
  620.     rs.audio = NULL;
  621.     rs.video = NULL;
  622.   rs.hPlayerWnd = NULL;
  623.  
  624.   // Initialize video renderer
  625.   rs.pVideoRenderer = new CVideoRenderer(hWnd, false, 0);
  626.   // Set the background logo
  627.   HBITMAP hbm=        LoadBitmap( hInst, MAKEINTRESOURCE(IDB_FONDO));
  628.   frBackground.GetFromBmp(hbm);
  629.   // The frame makes its own copy of the bitmap, so you can close the
  630.   // original now
  631.   DeleteObject(hbm);
  632.   // Set the bitmap as a background for the video renderer
  633.   rs.pVideoRenderer->SetStaticFrame(&frBackground);
  634.  
  635.  
  636.   // g_pBackground = new CBackground;
  637.   // g_pBackground->Start( rs.pVideoRenderer, &frBackground );
  638.  
  639.     rs.lng->SetLanguage( rs.conf.selected_language );
  640.     SetTextMenu();
  641.     rs.working_mode      =  FILE_MODE;
  642.  
  643.   ShowWindow(hWnd, nCmdShow);
  644.  
  645.   InvalidateRect(hWnd, NULL, TRUE);
  646.  
  647.   DisableMenu();
  648.    
  649.    //hdd=DrawDibOpen();
  650.    return TRUE;
  651. }
  652.  
  653. //
  654. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  655. //
  656. //  PURPOSE:  Processes messages for the main window.
  657. //
  658. //  WM_COMMAND    - process the application menu
  659. //  WM_PAINT    - Paint the main window
  660. //  WM_DESTROY    - post a quit message and return
  661. //
  662. //
  663. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  664. {
  665.     int wmId, wmEvent;
  666.     PAINTSTRUCT ps;
  667.     HDC hdc;
  668.     TCHAR szHello[MAX_LOADSTRING];
  669.   HWND hDlg;
  670.  
  671.     LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
  672.  
  673.  
  674.     switch (message) 
  675.     {
  676.         case WM_COMMAND:
  677.             wmId    = LOWORD(wParam); 
  678.             wmEvent = HIWORD(wParam);
  679.  
  680.             // HANDLING CUSTOM MENUES
  681.       // LastFiles list
  682.             if(wmId >= 10500 && wmId <=10503)
  683.       {
  684.         rs.profiler->AddNSelect( HidePlayer() );
  685.         DisableMenu();
  686.                 if(OpenFile( rs.conf.lastFiles[wmId-10500] ))
  687.         {
  688.           EnableMenu();
  689.           // g_pBackground->Stop();
  690.           rs.pVideoRenderer->StartPlaying();
  691.          // clear jobs
  692.           rs.vBatchList.clear(); 
  693.           ShowPlayer(rs.profiler->GetSelected() );
  694.         }
  695.       }
  696.  
  697.             // Language
  698.             if(wmId >= 10000 && wmId <10256){
  699.                 rs.lng->SetLanguage(wmId-10000);
  700.                 SetTextMenu();
  701.             }
  702.             // Plugins
  703.             if(wmId >= 10300 && wmId <(10300+MAX_OUT_PLUGINS) ){
  704.                 rs.selected_out_plug = wmId - 10300;
  705.                 SetTextMenu();
  706.             }
  707.       // Profiles
  708.             if(wmId >= 10400 && wmId <10499 ){
  709.         rs.profiler->AddNSelect( HidePlayer() );
  710.  
  711.                 rs.profiler->Select( wmId - 10400 );
  712.                 rs.prof = *rs.profiler->GetSelected();
  713.                 SetTextMenu();
  714.  
  715.                 ShowPlayer(rs.profiler->GetSelected() );
  716.       }
  717.  
  718.             // Parse the menu selections:
  719.             switch (wmId)
  720.             {
  721.  
  722.                 case IDM_OPENFILE:
  723.                     if( OpenFileDialog(rs.openedFileName) )
  724.           { 
  725.             rs.profiler->AddNSelect( HidePlayer() );
  726.             DisableMenu();
  727.             if( OpenFile( rs.openedFileName ) ) 
  728.             {
  729.               EnableMenu();
  730.               // g_pBackground->Stop();
  731.               rs.pVideoRenderer->StartPlaying();
  732.               //rs.video->BuildFrameDB();
  733.               // clear jobs
  734.               rs.vBatchList.clear();
  735.               InsertFileInLastFilesList( rs.openedFileName );
  736.               SetTextMenu();
  737.               ShowPlayer(rs.profiler->GetSelected() );
  738.             }
  739.           }
  740.                     break;
  741.                 case IDM_MPEG1OPTIONS:
  742.           rs.obOutputManager.ConfigureOutput(rs.selected_out_plug);
  743.           FlushPluginSettings( &rs.plugs );
  744.                   break;
  745.                 case IDM_START2:
  746.           rs.profiler->AddNSelect( HidePlayer() );
  747.           TOutputManagerRunInfo omri;
  748.  
  749.           omri.pJobList = &rs.vBatchList;
  750.           omri.pReport  = &rs.obProgressReportDlg;
  751.           omri.prof = *rs.profiler->GetSelected();
  752.  
  753.           if(rs.obOutputManager.Run(&omri))
  754.           {
  755.              DisableAllMenu();
  756.              hDlg = CreateDialogParam( hInst, MAKEINTRESOURCE(IDD_PROGRESS), hWnd, 
  757.                                          (DLGPROC)ProgressDlg, (LPARAM)&rs.obOutputManager); 
  758.              // Set as progress report
  759.              rs.obProgressReportDlg.SetDlg(hDlg);
  760.  
  761.              // Get post processing options from config
  762.              
  763.              TPPost pps;
  764.              FlPostProcess pp;
  765.              FromConfigToPPost( rs.profiler->GetSelected(), &pps, 
  766.                                 rs.video->GetWidth(), 
  767.                                 rs.video->GetHeight(),
  768.                                 rs.video->DAR,
  769.                                 FRAME_YV12 );           
  770.              pp.Set( &pps );
  771.              
  772.              WindowClientResize( hMainWnd, pp.GetWidth() , pp.GetHeight() );
  773.  
  774.              ShowWindow( hDlg, SW_SHOW);
  775.           }
  776.           
  777.                     break;
  778.                 case IDM_PLAY:
  779.           ShowPlayer(rs.profiler->GetSelected() );                
  780.                     break;
  781.                 case IDM_AUDIOPLAY:
  782.                     break;
  783.                 case IDM_OUTPUTCONFIG:
  784.           if( DialogBoxParam(hInst, (LPCTSTR)IDD_OUTPUT_OPTIONS, hWnd, (DLGPROC)OutputOptionsDlg,
  785.               (LPARAM)GetCurrentPlayerSettings() )!=IDCANCEL)
  786.           {
  787.             // Discard player settings here
  788.             HidePlayer();
  789.             SetTextMenu();
  790.             ShowPlayer(rs.profiler->GetSelected() );
  791.           }
  792.                     break;
  793.         case IDM_EXTRACTAUDIO:
  794.           rs.profiler->AddNSelect( HidePlayer() );
  795.           ExportAudio();
  796.           ShowPlayer(rs.profiler->GetSelected() );
  797.           break;
  798.                 case IDM_ABOUT:
  799.                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  800.                    break;
  801.         case IDM_HOMEPAGE:
  802.            ShellExecute(NULL, "open", "http://go.to/flaskmpeg", NULL, NULL, SW_SHOWNORMAL);
  803.            break;
  804.         case IDM_README:
  805.           ShellExecute(NULL, "open", "./docs/readme.html", NULL, NULL, SW_SHOWNORMAL);
  806.           break;          
  807.                 case IDM_EXIT:
  808.            rs.profiler->AddNSelect( HidePlayer() );
  809.                    DestroyWindow(hWnd);
  810.                    break;
  811.                 default:
  812.                    return DefWindowProc(hWnd, message, wParam, lParam);
  813.             }
  814.             break;
  815.         case WM_DROPFILES:
  816.             rs.profiler->AddNSelect( HidePlayer() );
  817.             DisableMenu();
  818.             char dropFileName[MAXFILENAME];
  819.  
  820.             DragQueryFile( (HDROP)wParam, 0/*first file*/,dropFileName,MAXFILENAME); 
  821.  
  822.  
  823.             if(OpenFile(dropFileName))
  824.             {
  825.               EnableMenu();
  826.               // clear jobs
  827.               rs.vBatchList.clear();
  828.               InsertFileInLastFilesList( rs.openedFileName );
  829.               SetTextMenu();
  830.               ShowPlayer(rs.profiler->GetSelected() );            
  831.             }
  832.  
  833.  
  834.             break;
  835.     case WM_MOVE:
  836.       // Snap the player at the botton of the main window
  837.       if(rs.hPlayerWnd)
  838.       {
  839.         RECT rcMain;
  840.         GetWindowRect( hMainWnd, &rcMain );
  841.         WindowMove( rs.hPlayerWnd, rcMain.left, rcMain.bottom );
  842.       }
  843.       if( rs.pVideoRenderer ) rs.pVideoRenderer->UpdateOverlay();
  844.  
  845.       break;
  846.         case WM_PAINT:
  847.             hdc = BeginPaint(hWnd, &ps);
  848.       rs.pVideoRenderer->Update();
  849.             EndPaint(hWnd, &ps);
  850.             break;
  851.         case WM_DESTROY:
  852.       
  853.       rs.profiler->AddNSelect( HidePlayer() );
  854.  
  855.       // g_pBackground->Stop();
  856.  
  857.       rs.conf.selected_profile  = rs.profiler->GetSelectedIndex();
  858.             rs.conf.selected_language = rs.lng->GetLanguage();
  859.             rs.conf.outputFormat      = rs.selected_out_plug;
  860.       
  861.       rs.profiler->AddProfile(&rs.prof);
  862.  
  863.             SaveRegistryConfig(&rs.conf);
  864.             ClosePlugins(&rs.plugs);
  865.       StopMism();
  866.  
  867.       if( rs.video )
  868.         delete rs.video;
  869.       if( rs.audio )
  870.         delete rs.audio;
  871.       if( rs.pVideoRenderer )
  872.         delete rs.pVideoRenderer;
  873.  
  874.             PostQuitMessage(0);
  875.             break;
  876.         default:
  877.             return DefWindowProc(hWnd, message, wParam, lParam);
  878.    }
  879.    return 0;
  880. }
  881.  
  882. // Mesage handler for about box.
  883. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  884. {
  885.   char szTemp[256];
  886.     switch (message)
  887.     {
  888.         case WM_INITDIALOG:
  889.       // Change the title to current version
  890.       sprintf( szTemp, "FlasKMPEG %d.%d.%d.%d", FLASK_VERSION>>24, 
  891.         (FLASK_VERSION>>16)&0xFF, 
  892.         (FLASK_VERSION>>8)&0xFF,
  893.          FLASK_VERSION&0xFF);
  894.       DlgSetText( hDlg, IDC_ABOUTVERSION, szTemp );
  895.                 SetText(hDlg, GS(ABOUT_TITLE) );
  896.                 DlgSetText( hDlg, R_ABOUT_GPL, GS(R_ABOUT_GPL) );
  897.                 DlgSetText( hDlg, R_ABOUT_TRANSLATOR, GS(R_ABOUT_TRANSLATOR) );
  898.                 DlgSetText( hDlg, R_ABOUT_DROPLINE, GS(R_ABOUT_DROPLINE) );
  899.                 DlgSetText( hDlg, R_ABOUT_ATHLON, GS(R_ABOUT_ATHLON) );
  900.  
  901.                 return TRUE;
  902.  
  903.         case WM_COMMAND:
  904.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  905.             {
  906.                 EndDialog(hDlg, LOWORD(wParam));
  907.                 return TRUE;
  908.             }
  909.             break;
  910.     }
  911.     return FALSE;
  912. }
  913.  
  914.  
  915.  
  916.  
  917.  
  918.