home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / FlasKMPEG.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-26  |  49.7 KB  |  1,699 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. /*extern "C"{*/
  27. #include "FlasKMPEG.h"
  28. /*}*/
  29. #include ".\Demux\Demux.h"
  30. #include ".\Video\VideoWrapper.h"
  31. #include ".\Audio\Audio.h"
  32. #include ".\Input\StreamDetector.h"
  33. #include ".\Input\DVDSelector.h"
  34. #include ".\plugins.h"
  35. #include ".\error.h"
  36. #include ".\RunState.h"
  37. #include ".\language.h"
  38. #include ".\Misc\Graph.h"
  39. #include ".\FileOpen.h"
  40. #include ".\Audio\AudioPlayback.h"
  41.  
  42. #include "YUVtoRGB.h"
  43. #include <malloc.h>
  44. #include <commdlg.h>
  45. #include <CommCtrl.h> 
  46. #include "resource.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <ctype.h>
  50. #include <fcntl.h>
  51. #include <math.h>
  52.  
  53. #include ".\Resizer\filter.h"
  54. #include ".\Resizer\resizer.h"
  55. #include ".\postprocessing.h"
  56. #include ".\windebug.h"
  57. #include "auxiliary.h"
  58. #include "mmx.h"
  59.  
  60. #define MAX_LOADSTRING 100
  61. #define TESTING_IFO_PARSER
  62. //#define TESTING_AUDIO_OUTPUT
  63. //#define TESTING_MULTIAUDIO
  64. // Global Variables:
  65. extern "C" BOOL FPU_enabled;
  66.  
  67. BOOL FPU_enabled;
  68. HINSTANCE            hInst;                                // current instance
  69. HWND                hConsole;
  70. TCHAR                szTitle[MAX_LOADSTRING];                                // The title bar text
  71. TCHAR                szWindowClass[MAX_LOADSTRING];                                // The title bar text
  72. HANDLE                hThrd;
  73. DWORD                    IDThread, ENCIDThread; 
  74. HANDLE                hEncodingThread;
  75. UINT                progressTimer;
  76.  
  77.  
  78. unsigned char       *newblock;
  79.  
  80. TRunState rs;
  81.  
  82. VBitmap                Display;
  83. VBitmap                DecodedImage;
  84.  
  85. HWND                  hMainWnd;
  86. BITMAPINFOHEADER    DibHeader;
  87. BITMAPINFO            DibInfo;
  88. BOOL                playerStopped;
  89. BOOL                 abortCompile;        //Use this to abort the compile
  90. BOOL                 audioFinished;
  91. BOOL                compileAborted;     //Use this to kill progress dialog
  92. BOOL                decodingFinished;    //The decoding thread has finished
  93. //framerate sync variables
  94. i64 myClock;
  95. static i64 frameSpan;
  96. bool   imageWaiting;
  97. bool   firstTime;
  98. bool   firstImageAlreadyDecoded=false;
  99. presInfo pInfo;
  100. // Compiling globals
  101. int        audioPrevFrame;
  102. bool    pauseCompile;
  103. bool    compileIsPaused;
  104.  
  105. TFileProgress        FileProgress;
  106.  
  107. extern void InitPremiereCallbacks();
  108. extern compStdParms        stdParms;
  109.  
  110. typedef enum{LOAD_LIBRARY,
  111.      ENCODE,
  112.     UNLOAD_LIBRARY}        EncEnum;
  113. EncEnum        sendEncodingThread;
  114.  
  115. unsigned char       DisplayImage[MAX_IMAGE_MEM];
  116. unsigned char        MPEGImage[MAX_IMAGE_MEM];
  117.  
  118. char                *chars="-\\|/-\\|/";
  119.  
  120. // 
  121. #ifdef TESTING_AUDIO_OUTPUT
  122.     FILE    *test_sound_file;
  123. #endif
  124. //  Output Options
  125. TConfig o;
  126.     
  127.  
  128.  
  129.  
  130.  
  131.  
  132. // Premiere Related Globals
  133. compGetFilePrefsRec     FilePrefsRec;    //For private settings of plugin
  134. compDoCompileInfo       CompileInfo;                    //For Compiling...
  135.  
  136.  
  137.  
  138. // Foward declarations of functions included in this code module:
  139. ATOM                MyRegisterClass(HINSTANCE hInstance);
  140. BOOL                InitInstance(HINSTANCE, int);
  141. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  142. LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  143. LRESULT CALLBACK    OutputOptionsDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  144. LRESULT CALLBACK    ProgressDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  145. LRESULT CALLBACK    DlgConsole(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  146. LRESULT CALLBACK    PlayerDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  147.                void SetTextMenu();
  148.  
  149. void                EncoderMessage( int message );
  150. DWORD WINAPI        EncodingThread(LPVOID *nada);
  151.  
  152.  
  153. int APIENTRY WinMain(HINSTANCE hInstance,
  154.                      HINSTANCE hPrevInstance,
  155.                      LPSTR     lpCmdLine,
  156.                      int       nCmdShow)
  157. {
  158.      // TODO: Place code here.
  159.     MSG msg;
  160.     HACCEL hAccelTable;
  161.  
  162.     rs.lng = new CLanguage;
  163.     if(rs.lng){
  164.         rs.lng->Start();
  165.     }
  166.  
  167.     // Initialize global strings
  168.     strcpy( szTitle, GS(APPTITLE) );
  169. //    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  170.     LoadString(hInstance, IDC_FLASKMPG2MPG1, szWindowClass, MAX_LOADSTRING);
  171.     MyRegisterClass(hInstance);
  172.  
  173.     // Perform application initialization:
  174.     if (!InitInstance (hInstance, nCmdShow)) 
  175.     {
  176.         return FALSE;
  177.     }
  178.  
  179.     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_FLASKMPG2MPG1);
  180.  
  181.     // Main message loop:
  182.     while (GetMessage(&msg, NULL, 0, 0)) 
  183.     {
  184.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  185.         {
  186.             TranslateMessage(&msg);
  187.             DispatchMessage(&msg);
  188.         }
  189.     }
  190.  
  191.     return msg.wParam;
  192. }
  193.  
  194.  
  195. //   BITMAP DISPLAY INIT
  196. void InitDibDisplay(int oxsize, int oysize){
  197. RECT wr,cr;
  198. int  d;
  199.                Display.init(DisplayImage,  oxsize, oysize, 32);
  200.                Display.MakeBitmapHeader(&DibHeader);
  201.  
  202.               memcpy(&DibInfo.bmiHeader, &DibHeader, sizeof(BITMAPINFOHEADER));
  203.               // Set Window Size
  204.  
  205.             // size window to film size
  206.             SetRect(&cr,0,0,oxsize, oysize);
  207.             wr=cr;
  208.             AdjustWindowRect(&wr,GetWindowLong(hMainWnd,GWL_STYLE),TRUE);
  209.             OffsetRect(&wr,-wr.left,-wr.top);
  210.             SetWindowPos(hMainWnd,HWND_TOP,0,0,wr.right,wr.bottom,SWP_NOMOVE);
  211.  
  212.             // for QSIF movies the menu will wrap around so we need
  213.             // to increase the height of the window
  214.             GetClientRect(hMainWnd, &wr);
  215.             d=cr.bottom-(wr.bottom-wr.top);
  216.  
  217.             if (d>0)
  218.             {
  219.               GetWindowRect(hMainWnd, &wr);
  220.               SetWindowPos(hMainWnd,HWND_TOP,0,0,wr.right-wr.left,wr.bottom-wr.top+d,SWP_NOMOVE);
  221.             }
  222.  
  223. }
  224.  
  225. char * add_file_ext(char *file){
  226.     if(!file)
  227.         return NULL;
  228.             if(rs.plugs.outPlugs[rs.selected_out_plug].fileType==VXXtype)
  229.                 strcat(file,".avi");
  230.             if(rs.plugs.outPlugs[rs.selected_out_plug].fileType=='MPEG'){
  231.                 if(o.options.audioMode==DO_AUDIO)
  232.                     strcat(file,".mpg");
  233.                 else
  234.                     strcat(file,".m1v");
  235.             }
  236.             if(rs.plugs.outPlugs[rs.selected_out_plug].fileType=='\0MPG'){
  237.                 if(o.options.audioMode==DO_AUDIO)
  238.                     strcat(file,".mpg");
  239.                 else
  240.                     strcat(file,".m1v");
  241.             }
  242.     return file;
  243. }
  244. void SetWindowSize( void )
  245. {
  246. RECT cr,wr;
  247. int d;
  248.             // size window to film size
  249.             SetRect(&cr,0,0,352, 288);
  250.             wr=cr;
  251.             AdjustWindowRect(&wr,GetWindowLong((HWND)hMainWnd,GWL_STYLE),TRUE);
  252.             OffsetRect(&wr,-wr.left,-wr.top);
  253.             SetWindowPos((HWND)hMainWnd,HWND_TOP,0,0,wr.right,wr.bottom,SWP_NOMOVE);
  254.  
  255.             // for QSIF movies the menu will wrap around so we need
  256.             // to increase the height of the window
  257.             GetClientRect((HWND)hMainWnd, &wr);
  258.             d=cr.bottom-(wr.bottom-wr.top);
  259.  
  260.             if (d>0)
  261.             {
  262.               GetWindowRect((HWND)hMainWnd, &wr);
  263.               SetWindowPos((HWND)hMainWnd,HWND_TOP,0,0,wr.right-wr.left,wr.bottom-wr.top+d,SWP_NOMOVE);
  264.             }
  265. }
  266.  
  267.  
  268.  
  269. //
  270. //  FUNCTION: MyRegisterClass()
  271. //
  272. //  PURPOSE: Registers the window class.
  273. //
  274. //  COMMENTS:
  275. //
  276. //    This function and its usage is only necessary if you want this code
  277. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  278. //    function that was added to Windows 95. It is important to call this function
  279. //    so that the application will get 'well formed' small icons associated
  280. //    with it.
  281. //
  282. ATOM MyRegisterClass(HINSTANCE hInstance)
  283. {
  284.     WNDCLASSEX wcex;
  285.  
  286.     wcex.cbSize = sizeof(WNDCLASSEX); 
  287.  
  288.     wcex.style            = CS_HREDRAW | CS_VREDRAW;
  289.     wcex.lpfnWndProc    = (WNDPROC)WndProc;
  290.     wcex.cbClsExtra        = 0;
  291.     wcex.cbWndExtra        = 0;
  292.     wcex.hInstance        = hInstance;
  293.     wcex.hIcon            = (HICON)LoadImage(hInstance, (LPCTSTR)IDI_FLASKMPG2MPG1, IMAGE_ICON, 0,0,0);
  294.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  295.     wcex.hbrBackground    = (HBRUSH) GetStockObject(BLACK_BRUSH);
  296.     wcex.lpszMenuName    = (LPCSTR)IDC_FLASKMPG2MPG1;
  297.     wcex.lpszClassName    = szWindowClass;
  298.     wcex.hIconSm        = NULL;//LoadIcon(wcex.hInstance, (LPCTSTR)IDI_FLASKMPG2MPG1);
  299.  
  300.     return RegisterClassEx(&wcex);
  301. }
  302.  
  303. //
  304. //   FUNCTION: InitInstance(HANDLE, int)
  305. //
  306. //   PURPOSE: Saves instance handle and creates main window
  307. //
  308. //   COMMENTS:
  309. //
  310. //        In this function, we save the instance handle in a global variable and
  311. //        create and display the main program window.
  312. //
  313. // Mesage handler for about box.
  314. LRESULT CALLBACK WelcomeDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  315. {
  316.     int n_lang, i;
  317.     switch (message)
  318.     {
  319.         case WM_INITDIALOG:
  320.                 //Language Selection
  321.                 DlgSetText(hDlg, R_WELCOME_LANG, GS(MENU_LANGUAGE));
  322.                 n_lang = rs.lng->GetNumberLang();
  323.                 for(i=0; i<n_lang; i++){
  324.                     rs.lng->SetLanguage(i);
  325.                     ListAddText(GetDlgItem(hDlg, R_LANG_LIST), rs.lng->GetLanguageID() );
  326.                 }
  327.                 ListSetCur(GetDlgItem(hDlg, R_LANG_LIST), 0);
  328.                 return TRUE;
  329.  
  330.         case WM_COMMAND:
  331.  
  332.  
  333.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  334.             {
  335.                 EndDialog(hDlg, LOWORD(wParam));
  336.                 return TRUE;
  337.             }
  338.             if(HIWORD(wParam)==CBN_SELCHANGE){
  339.                 rs.lng->SetLanguage( SendDlgItemMessage(hDlg, R_LANG_LIST, CB_GETCURSEL, 0,0) );
  340.                 o.options.selected_language = rs.lng->GetLanguage();
  341.                 SetTextMenu();
  342.                 return TRUE;
  343.             }
  344.             break;
  345.     }
  346.     return FALSE;
  347. }
  348.  
  349. void SetTextMenu(){
  350.     HMENU menu;
  351.     int i;
  352.     
  353.     MenuSetText(GetMenu(hMainWnd), 0, GS(MENU_FILE));
  354.     MenuSetText(GetMenu(hMainWnd), 1, GS(MENU_OPTIONS));
  355.     MenuSetText(GetMenu(hMainWnd), 2, GS(MENU_RUN));
  356.     MenuSetText(GetMenu(hMainWnd), 3, GS(MENU_ABOUT));
  357.  
  358.     // File preferences
  359.     menu = MenuGetPopup(GetMenu(hMainWnd), 0);
  360.     MenuSetText( menu, 0, GS(MENU_OPEN));
  361.     MenuSetText( menu, 1, GS(MENU_OPENDVDFILE));
  362.     MenuSetText( menu, 3, GS(MENU_EXIT));
  363.  
  364.     // Options preferences
  365.     menu = MenuGetPopup(GetMenu(hMainWnd), 1);
  366.     MenuSetText( menu, 0, GS(MENU_OUTPUTFORMAT));
  367.     MenuSetText( menu, 1, GS(MENU_OUTOPTIONS));
  368.     MenuSetText( menu, 3, GS(MENU_SELECTOUT));
  369.     MenuSetText( menu, 5, GS(MENU_LANGUAGE));
  370.  
  371.     // Output plugins
  372.     menu = MenuGetPopup(GetMenu(hMainWnd), 1);
  373.     menu = MenuGetPopup(menu, 3);
  374.     while(RemoveMenu(menu, 0, MF_BYPOSITION ));
  375.     
  376.     for(i=0; i<rs.plugs.OutPluginCount; i++){
  377.         MenuAddItem( menu, i, i+10300, rs.plugs.outPlugs[i].ID );
  378.     }
  379.     MenuCheck(hMainWnd, 10300+rs.selected_out_plug);
  380.  
  381.     //Languages menus
  382.         // Get languages popup
  383.     menu = MenuGetPopup(GetMenu(hMainWnd), 1);
  384.     menu = MenuGetPopup(menu, 5);
  385.  
  386.     while(RemoveMenu(menu, 0, MF_BYPOSITION ));
  387.     
  388.  
  389.     int current_lang = rs.lng->GetLanguage();
  390.     int n_lang = rs.lng->GetNumberLang();
  391.     for(i=0; i<n_lang; i++){
  392.         rs.lng->SetLanguage(i);
  393.         MenuAddItem(menu, i, i+10000, rs.lng->GetLanguageID() );
  394.     }
  395.  
  396.     rs.lng->SetLanguage(current_lang);
  397.  
  398.     //Run
  399.     menu = MenuGetPopup(GetMenu(hMainWnd), 2);
  400.     MenuSetText( menu, 0, GS(MENU_PLAYER));
  401.     MenuSetText( menu, 1, GS(MENU_STARTCONV));
  402.  
  403.  
  404.     DrawMenuBar( hMainWnd );
  405.  
  406.  
  407. }
  408. void SetMenuMPEG(){
  409.     MenuCheck(hMainWnd, IDM_MPEG);
  410.     MenuUnCheck(hMainWnd, IDM_AVI);
  411.     MenuEnable(hMainWnd,IDM_MPEG1OPTIONS);
  412. }
  413. void SetMenuAVI(){
  414.     MenuCheck(hMainWnd, IDM_AVI);
  415.     MenuUnCheck(hMainWnd, IDM_MPEG);
  416.     //MenuDisable(hMainWnd,IDM_MPEG1OPTIONS);
  417.     MenuEnable(hMainWnd,IDM_MPEG1OPTIONS);
  418. }
  419. void EnableMenu(){
  420.     MenuEnable(hMainWnd, IDM_PLAY);
  421.     MenuEnable(hMainWnd, IDM_MPEG1OPTIONS);
  422.  
  423.     MenuEnable(hMainWnd, IDM_START2);
  424.     MenuEnable(hMainWnd, IDM_OUTPUTCONFIG);
  425.     //EnableMenuItem(GetSubMenu(GetMenu(hMainWnd), 1), GetMenuItemID(GetSubMenu(GetMenu(hMainWnd), 1),3), MF_GRAYED);
  426.  
  427. }
  428. void DisableMenu(){
  429.     MenuDisable(hMainWnd, IDM_PLAY);
  430.     MenuDisable(hMainWnd, IDM_MPEG1OPTIONS);
  431.     MenuDisable(hMainWnd, IDM_START2);
  432.     MenuDisable(hMainWnd, IDM_OUTPUTCONFIG);
  433. //    MenuDisable(hMainWnd, IDM_DISPLAYVIDEO);
  434.     MenuDisable(hMainWnd, IDM_MPEG);
  435.     MenuDisable(hMainWnd, IDM_AVI);
  436. }
  437. void DisableAllMenu(){
  438.     DisableMenu();
  439.     MenuDisable(hMainWnd, IDM_OPENFILE);
  440.     MenuDisable(hMainWnd, IDM_EXIT);
  441. }
  442. void EnableAllMenu(){
  443.     EnableMenu();
  444.     MenuEnable(hMainWnd, IDM_OPENFILE);
  445.     MenuEnable(hMainWnd, IDM_EXIT);
  446. }
  447. void LoadFirstTime(HINSTANCE hInstance, HWND hWnd){
  448.  
  449.     o.version=VERSION;
  450.     o.options.showDetailsInProgress=true;
  451.     o.options.idctType= IDCT_MMX;
  452.     o.options.selectedTab=0;
  453.  
  454.     o.options.warn_overwrite=   true;
  455.     o.options.shutdown      =   false;
  456.     o.options.lurk_size     =   5000;
  457.  
  458.     o.options.priority      =   NORMAL_PRIO;
  459.     o.options.crop            =    false;
  460.     o.options.cropTopOffset =    0;
  461.     o.options.cropHeight    =    288;
  462.     o.options.cropLeftOffset=    0;
  463.     o.options.cropWidth        =    352;
  464.  
  465.  
  466.     o.options.letterbox        =    false;
  467.     o.options.letterboxTop    =    0;
  468.     o.options.letterboxBottom=    0;
  469.     o.options.letterboxLeft    =    0;
  470.     o.options.letterboxRight=    0;
  471.  
  472.     o.options.deinterlace     =   0;
  473.     o.options.blend         =   1;
  474.     o.options.threshold     =  20;
  475.  
  476.     o.options.keepAspect                     = true;
  477.     o.options.recons_progressive             = true;
  478.  
  479.     o.options.InterpolatedWidth=352;
  480.     o.options.InterpolatedHeight=288;
  481.     o.options.compileWhole       = true;
  482.     o.options.framesToCompile                = 3000;
  483.     o.options.displayVideo                   = true;
  484.     o.options.filter                         = FILTER_TABLEBICUBIC;
  485.     o.options.hOutputFile                    = NULL;
  486.     o.options.timeBase.scale                 = 25;
  487.     o.options.timeBase.sampleSize            = 1;
  488.     o.options.timeBase.value                 = 0xFFFFFF;
  489.     o.options.presentFrame                   = 0;
  490.     o.options.outputFormat                   = 0;
  491.     o.options.syncVideo                      = true;
  492.     o.options.audioMode                      = DO_AUDIO;
  493.     o.options.outSampleFrequency             = 44100;
  494.     o.options.sampleFreqSameAsInput          = true;
  495.  
  496.     o.options.firstTimeRunning= true;
  497.  
  498.  
  499.     strcpy(o.options.audioOutFile, "C:\\audioOut");
  500.     strcpy(o.options.outputFile, "C:\\videoOut");
  501.     SaveRegistryConfig(&o);
  502.     LoadRegistryConfig(&o);
  503.     DialogBox(hInstance, MAKEINTRESOURCE(IDD_WELCOME), hWnd, (DLGPROC)WelcomeDlg);
  504. }
  505.  
  506.  
  507. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  508. {
  509.    HWND hWnd;
  510.  
  511.    hInst = hInstance; // Store instance handle in our global variable
  512.    hMainWnd=hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX ,
  513.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  514.  
  515.    if (!hWnd)
  516.    {
  517.       return FALSE;
  518.    }
  519.    //Load plugins
  520.     if(!LoadPlugins(&rs.plugs)){
  521.         PrintError(ERROR_PLUGIN, (int)hMainWnd, 0);
  522.         exit(0);
  523.     }
  524.  
  525.        //Starting options
  526.     if(LoadRegistryConfig(&o)==0)    //This is the first time. Load defaults
  527.     {
  528.         LoadFirstTime(hInstance, hWnd);
  529.     }
  530.     else
  531.     {
  532.         //Key existed. Check version
  533.         if(o.version!=VERSION)
  534.             LoadFirstTime(hInstance, hWnd);
  535.         else
  536.             o.options.firstTimeRunning=false;
  537.     }
  538.     if(rs.plugs.OutPluginCount <= o.options.outputFormat)
  539.         o.options.outputFormat = 0;
  540.     rs.selected_out_plug = o.options.outputFormat;
  541.     SetTextMenu();
  542.     //Miscelaneous Inits
  543.     SetWindowSize();
  544.     compileAborted=true;
  545.     playerStopped=true;
  546.     rs.startFilePos=0;
  547.     rs.SD=NULL;
  548.     rs.audio = NULL;
  549.     rs.video = NULL;
  550.     rs.video_inp    = NULL;
  551.     rs.audio_inp    = NULL;
  552.     rs.lng->SetLanguage( o.options.selected_language );
  553.     SetTextMenu();
  554.     rs.working_mode      =  FILE_MODE;
  555.  
  556.     hEncodingThread = CreateThread(NULL,  // no security attributes 
  557.                          0,                    // use default stack size 
  558.                          (LPTHREAD_START_ROUTINE) EncodingThread, 
  559.                          (LPVOID)NULL, // param to thread func 
  560.                          0, // creation flag 
  561.                          &ENCIDThread);       // thread identifier     
  562.     if(hEncodingThread)
  563.         EncoderMessage( LOAD_LIBRARY );
  564.  
  565.  
  566.    ShowWindow(hWnd, nCmdShow);
  567.  
  568.    UpdateWindow(hWnd);
  569.     MMX_check();
  570.     if(MMX_capable)
  571.         MMX_enable(1);
  572.    
  573.    //hdd=DrawDibOpen();
  574.    return TRUE;
  575. }
  576.  
  577.  
  578.  
  579.  
  580.  
  581. Crop(ui8 *image, int xsize,int ysize,int topCrop,int bottomCrop){
  582.  
  583.     int topStart;
  584.     ui8 *imagetemp;
  585.  
  586. if(topCrop>ysize)
  587.     topCrop=ysize;
  588. if(bottomCrop>ysize)
  589.     bottomCrop=ysize;
  590. // First byte, image bottom    
  591. ZeroMemory(image,         bottomCrop*xsize*4);
  592. // bottom
  593.     topStart=ysize-topCrop;
  594.  
  595.     imagetemp = image         + topStart*xsize*4;
  596.     ZeroMemory(imagetemp, topCrop*xsize*4);
  597.  
  598.  
  599.     return 1;
  600. }
  601.  
  602. //
  603. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  604. //
  605. //  PURPOSE:  Processes messages for the main window.
  606. //
  607. //  WM_COMMAND    - process the application menu
  608. //  WM_PAINT    - Paint the main window
  609. //  WM_DESTROY    - post a quit message and return
  610. //
  611. //
  612. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  613. {
  614.     int wmId, wmEvent;
  615.     PAINTSTRUCT ps;
  616.     HDC hdc;
  617.     TCHAR szHello[MAX_LOADSTRING];
  618.  
  619.     RECT rt;
  620.     HDC hdcCompatible;
  621.     HBITMAP hbm;
  622.     HWND hDlg;
  623.     HGDIOBJ hprevious;
  624.     TVideoOptions    video_opt;
  625.     LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
  626.     CompileEntryFunc CompileEntry;
  627.  
  628.  
  629.     switch (message) 
  630.     {
  631.         case WM_COMMAND:
  632.             wmId    = LOWORD(wParam); 
  633.             wmEvent = HIWORD(wParam);
  634.             // Handling custom menus
  635.             // Language
  636.             if(wmId >= 10000 && wmId <10256){
  637.                 rs.lng->SetLanguage(wmId-10000);
  638.                 SetTextMenu();
  639.             }
  640.             // Plugins
  641.             if(wmId >= 10300 && wmId <(10300+MAX_OUT_PLUGINS) ){
  642.                 rs.selected_out_plug = wmId - 10300;
  643.                 SetTextMenu();
  644.             }
  645.  
  646.             // Parse the menu selections:
  647.             switch (wmId)
  648.             {
  649.  
  650.                 case IDM_OPENFILE:
  651.                     OpenFile();
  652.                     break;
  653.                 case IDM_OPENDVD:
  654.                     if(!OpenDVDFile())
  655.                         PrintError(IFO_PARSING_ERROR,(int)hMainWnd, 0);
  656.                     break;
  657.                 case IDM_MPEG1OPTIONS:
  658.                         CompileEntry = rs.plugs.outPlugs[rs.selected_out_plug].CompileEntry;
  659.                 //User Preferences
  660.                     
  661.                     if(CompileEntry){
  662.                         FilePrefsRec.compilerPrefs = rs.plugs.outPlugs[rs.selected_out_plug].settings;
  663.                         (CompileEntry)(compGetFilePrefs, &stdParms, (LONG)&FilePrefsRec,0);
  664.                         if( FilePrefsRec.compilerPrefs ){
  665.                             if(!rs.plugs.outPlugs[rs.selected_out_plug].settings)
  666.                                 rs.plugs.outPlugs[rs.selected_out_plug].settings = (char *)malloc(OUT_PLUG_MEM);
  667.                             
  668.                             //ZeroMemory( rs.plugs.outPlugs[rs.selected_out_plug].settings , OUT_PLUG_MEM);
  669.                             memcpy( rs.plugs.outPlugs[rs.selected_out_plug].settings , FilePrefsRec.compilerPrefs, _msize(FilePrefsRec.compilerPrefs) );
  670.                             
  671.                         }
  672.                         
  673.                     }
  674.                     //In FilePrefsRec lay Plugin options
  675.                    break;
  676.                 case IDM_START2:
  677.  
  678.                     // Check for output files
  679.                     char  myFile[1024];
  680.                     // Init Demuxer
  681.                     myClock=0;
  682.  
  683.                     DecodedImage.init(&MPEGImage, rs.video->pictureWidth, rs.video->picutreHeight, 32);
  684.                     DecodedImage.AlignTo4();
  685.                     
  686.                     if(o.options.warn_overwrite){
  687.                         FILE *input = NULL;
  688.                         strcpy( myFile, o.options.audioOutFile);
  689.                         if(input=fopen(add_file_ext(myFile), "rb")){
  690.                             fclose(input);
  691.                             if(MessageBox(hWnd, GS(WARN_OVERWRITE), GS(WARNING), MB_YESNO) == IDNO)
  692.                                 break;
  693.                         }
  694.                         strcpy( myFile, o.options.outputFile);
  695.                         if(input=fopen(add_file_ext(myFile), "rb")){
  696.                             fclose(input);
  697.                             if(MessageBox(hWnd, GS(WARN_OVERWRITE), GS(WARNING), MB_YESNO) == IDNO)
  698.                                 break;
  699.                         }
  700.  
  701.                     }
  702. #ifdef TESTING_AUDIO_OUTPUT                
  703.                     test_sound_file = fopen("c:\\salida","wb");
  704. #endif
  705. #ifdef TESTING_MULTIAUDIO
  706.                 if(o.options.outputFormat==AVI_PLUG)
  707.                     (CompileEntry)(99, &stdParms, 2, 0);
  708. #endif
  709.                 // Get post processing options from config
  710.                 GetPPostConfig(&o, &rs.pp);
  711.                 // Set additional parameters
  712.                 rs.pp.iDAR = rs.video->DAR;
  713.  
  714.                 InitDibDisplay(PPOST_WIDTH(rs.pp) , PPOST_HEIGHT(rs.pp));
  715.                 // Start Post processing
  716.                 PostProcessingStart(&DecodedImage, &Display, &rs.pp);
  717.                 rs.pp.myClock = myClock;
  718.  
  719.  
  720.  
  721.                     frameSpan= (i64)((1/((double)(o.options.timeBase.scale)/
  722.                          (double)(o.options.timeBase.sampleSize)))*(double)MPEG2_CLK_REF);
  723.                     imageWaiting=false;
  724.                     firstTime=true;
  725.                     audioPrevFrame=0;
  726.                     //Seek mobs at the begginnig of the compile
  727.                     rs.video->SeekVideo(rs.startFilePos);
  728.                     if(rs.audio)
  729.                         rs.audio->SeekAudio(rs.startFilePos);
  730.                     #ifdef TESTING_MULTIAUDIO
  731.                         rs.audio2->SeekAudio(rs.startFilePos);
  732.                     #endif
  733.                     //Start video
  734.                     video_opt.idctType             = o.options.idctType;
  735.                     video_opt.recons_progressive   = o.options.recons_progressive;
  736.                     if(rs.video) rs.video->Start(&video_opt);
  737.                         else return 0;
  738.                         //Decode first frame to get PTS info for audio
  739.                         //video->GetFrame(&pInfo);
  740.                         firstImageAlreadyDecoded=false;
  741.                     if(o.options.sampleFreqSameAsInput)
  742.                         o.options.outSampleFrequency=rs.audio->sampleRate;
  743.                     //Start audio with that PTS info.
  744.                     if(rs.audio) {
  745.                         switch( o.options.audioMode){
  746.                         case DSC:
  747.                             rs.audio->Start(o.options.audioOutFile, o.options.audioMode);
  748.                             break;
  749.                         case DO_AUDIO:
  750.                             rs.audio->Start(o.options.outSampleFrequency,pInfo.imagePTS, o.options.audioMode);
  751.                             break;
  752.                         }
  753.                     }
  754.                     #ifdef TESTING_MULTIAUDIO
  755.                         rs.audio2->Start(o.options.outSampleFrequency,pInfo.imagePTS, o.options.audioMode);
  756.                     #endif
  757.  
  758.                     EncoderMessage( ENCODE );
  759.                     DisableAllMenu();
  760.                     hDlg=CreateDialog(hInst,MAKEINTRESOURCE(IDD_PROGRESS), hWnd, (DLGPROC) ProgressDlg);
  761.                     ShowWindow(hDlg, SW_SHOW);
  762.                     /*DialogBox(     hInst,  // handle to application instance
  763.                                    MAKEINTRESOURCE(IDD_PROGRESS),   // identifies dialog box template
  764.                                    hWnd,      // handle to owner window
  765.                                    (DLGPROC) ProgressDlg  // pointer to dialog box procedure
  766.                                    );
  767.                     */
  768.                     break;
  769. /*                case IDM_MPEG:
  770.                     o.options.outputFormat=MPEG_PAN;
  771.                     SetMenuMPEG();
  772.                     CompileEntry=PanEntry;
  773.                     break;
  774.                 case IDM_AVI:
  775.                     SetMenuAVI();
  776.                     CompileEntry=AVIplugEntry;
  777.                     o.options.outputFormat=AVI_PLUG;
  778.                     break;
  779. */                case IDM_PLAY:
  780.                     DialogBox( hInst, MAKEINTRESOURCE(IDD_PLAYER), hMainWnd, (DLGPROC)PlayerDlg);
  781.                     break;
  782.                 case IDM_OUTPUTCONFIG:
  783.                     DialogBox(hInst, (LPCTSTR)IDD_OUTPUT_OPTIONS, hWnd, (DLGPROC)OutputOptionsDlg);
  784.                     break;
  785.                 case IDM_ABOUT:
  786. /*                    TAudioPlayback ap;
  787.                     unsigned char *data;
  788.  
  789.                     ap.n_channels = 2;
  790.                     ap.sample_freq = rs.audio->sampleRate;
  791.                     ap.chunk_size = 4096*6;
  792.                     
  793.  
  794.  
  795.                     AudioPlaybackStart(&ap);
  796.                     rs.audio->Start(rs.audio->sampleRate, 0, DO_AUDIO);
  797.                     while(1)
  798.                     {
  799.                         rs.audio->GetSamples(0, (short **)&data, (4096*6)>>2);
  800.                         OutputDebugString("starting write");
  801.                         AudioPlaybackWrite(&ap, data);
  802.                         OutputDebugString("ending write");
  803.                     }
  804.                     AudioPlaybackStop(&ap);
  805. */
  806.                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  807.  
  808.                    break;
  809.                 case IDM_EXIT:
  810.  
  811.                    DestroyWindow(hWnd);
  812.                    break;
  813.                 default:
  814.                    return DefWindowProc(hWnd, message, wParam, lParam);
  815.             }
  816.             break;
  817.         case WM_PAINT:
  818.  
  819.  
  820.             hdc = BeginPaint(hWnd, &ps);
  821.             if( compileAborted && playerStopped ){
  822.             // TODO: Add any drawing code here...
  823.             hdcCompatible = CreateCompatibleDC(hdc); 
  824.             hbm=        LoadBitmap( hInst, MAKEINTRESOURCE(IDB_FONDO));
  825.             hprevious = SelectObject(hdcCompatible, hbm);
  826.             GetClientRect(hWnd, &rt);
  827.             BitBlt(hdc, 
  828.                     0,0, 
  829.                     rt.right-rt.left, rt.bottom-rt.top, 
  830.                     hdcCompatible, 
  831.                     0,0, 
  832.                     SRCCOPY); 
  833.             SelectObject( hdcCompatible, hprevious);
  834.             DeleteObject( hbm );
  835.             DeleteDC( hdcCompatible );
  836.             }
  837.             
  838.         //DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
  839.             EndPaint(hWnd, &ps);
  840.             break;
  841.         case WM_DESTROY:
  842.             o.options.selected_language = rs.lng->GetLanguage();
  843.             o.options.outputFormat      = rs.selected_out_plug;
  844.  
  845.             SaveRegistryConfig(&o);
  846.             FlushPluginSettings( &rs.plugs );
  847.             ClosePlugins(&rs.plugs);
  848.             EncoderMessage( UNLOAD_LIBRARY );
  849.             PostQuitMessage(0);
  850.             break;
  851.         default:
  852.             return DefWindowProc(hWnd, message, wParam, lParam);
  853.    }
  854.    return 0;
  855. }
  856.  
  857. // Mesage handler for about box.
  858. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  859. {
  860.     switch (message)
  861.     {
  862.         case WM_INITDIALOG:
  863.                 SetText(hDlg, GS(ABOUT_TITLE) );
  864.                 DlgSetText( hDlg, R_ABOUT_GPL, GS(R_ABOUT_GPL) );
  865.                 DlgSetText( hDlg, R_ABOUT_TRANSLATOR, GS(R_ABOUT_TRANSLATOR) );
  866.                 DlgSetText( hDlg, R_ABOUT_DROPLINE, GS(R_ABOUT_DROPLINE) );
  867.                 DlgSetText( hDlg, R_ABOUT_ATHLON, GS(R_ABOUT_ATHLON) );
  868.  
  869.                 return TRUE;
  870.  
  871.         case WM_COMMAND:
  872.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  873.             {
  874.                 EndDialog(hDlg, LOWORD(wParam));
  875.                 return TRUE;
  876.             }
  877.             break;
  878.     }
  879.     return FALSE;
  880. }
  881.  
  882. // Mesage handler for console
  883. LRESULT CALLBACK DlgConsole(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  884. {
  885.     switch (message)
  886.     {
  887.         case WM_INITDIALOG:
  888.                 return TRUE;
  889.  
  890.         case WM_COMMAND:
  891.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  892.             {
  893.                 EndDialog(hDlg, LOWORD(wParam));
  894.                 return TRUE;
  895.             }
  896.             break;
  897.     }
  898.     return FALSE;
  899. }
  900.  
  901. void SetPriority(int prio){
  902.     switch (prio){
  903.         case IDLE_PRIO:
  904.             SetThreadPriority(hEncodingThread, THREAD_PRIORITY_IDLE);
  905.             break;
  906.         case NORMAL_PRIO:
  907.             SetThreadPriority(hEncodingThread, THREAD_PRIORITY_NORMAL);
  908.             break;
  909.         case HIGH_PRIO:
  910.             SetThreadPriority(hEncodingThread, THREAD_PRIORITY_HIGHEST);
  911.             break;
  912.         case HIGHEST_PRIO:
  913.             SetThreadPriority(hEncodingThread, THREAD_PRIORITY_TIME_CRITICAL);
  914.             break;
  915.     }
  916. }
  917. // Mesage handler for about box.
  918. LRESULT CALLBACK ProgressDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  919. {
  920.     double porciento;
  921.     char szTemp[MAXFILENAME];
  922.     static char temp_filename[MAXFILENAME];
  923.     static int i,j,k,w,h;
  924.     static DWORD remaining_time,present_time,elapsed_time,start_time;
  925.     static __int64  startPos=0;
  926.     RECT rc;        
  927.  
  928.     switch (message)
  929.     {
  930.         case WM_SYSCOMMAND:
  931. /*            if(wParam==SC_MINIMIZE)
  932.                 SendMessage(hMainWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  933.             if(wParam==SC_RESTORE)
  934.                 SendMessage(hMainWnd, WM_SYSCOMMAND, SC_RESTORE, 0);*/
  935.         break;
  936.         case WM_TIMER:
  937.             if(!compileIsPaused){
  938.             
  939.             //Process information
  940.             //input
  941.             DlgSetText(hDlg, IDC_IVIDEORES, rs.video->sVideoSize);
  942.             DlgSetText(hDlg, IDC_IFRAMERATE, rs.video->sFrameRate);
  943.             DlgSetText(hDlg, IDC_IASPECT, rs.video->sAspectRatio);
  944.             DlgSetText(hDlg, IDC_IVIDEOSTRUCT ,rs.video->sProgressive);
  945.             DlgSetText(hDlg, IDC_IFILE, rs.video->GetVideoFileName());
  946.             if(rs.audio){
  947.                 (rs.audio->isAC3) ? (DlgSetText(hDlg, IDC_IAUDIO, "AC3 Stream")): (DlgSetText(hDlg, IDC_IAUDIO, "MPEG audio stream"));
  948.                 (rs.audio->isAC3) ? (DlgSetText(hDlg, IDC_IAUDIOFREQ, rs.audio->sSampleRate)): (DlgSetText(hDlg, IDC_IAUDIOFREQ, ""));
  949.                 (rs.audio->isAC3) ? (DlgSetText(hDlg, IDC_IAUDIOBITRATE, rs.audio->sBitRate)): (DlgSetText(hDlg, IDC_IAUDIOBITRATE, ""));
  950.                 (rs.audio->isAC3) ? (DlgSetText(hDlg, IDC_IAUDIOMODE, rs.audio->sAudioMode)): (DlgSetText(hDlg, IDC_IAUDIOMODE, ""));
  951.             }
  952.             // Graph
  953.             if(rs.plugs.outPlugs[rs.selected_out_plug].fileType == VXXtype)
  954.                 DrawGraph(hDlg, &rs.gr);
  955.             //outputs
  956.             sprintf(szTemp, "%d x %d pixels", PPOST_WIDTH(rs.pp), PPOST_HEIGHT(rs.pp));
  957.             DlgSetText( hDlg, IDC_OVIDEORES, szTemp);
  958.             StringFrameRate(o.options.timeBase.scale, szTemp);
  959.             DlgSetText( hDlg, IDC_OFRAMERATE, szTemp);
  960.             if(o.options.keepAspect) DlgSetText( hDlg, IDC_OKEEPASPECT, "Yes"); else DlgSetText( hDlg, IDC_OKEEPASPECT, "No");
  961.             DlgSetText( hDlg, IDC_OVIDEOFILE, temp_filename);
  962.             if(o.options.audioMode == DSC)
  963.                 DlgSetText( hDlg, IDC_OAUDIOFILE, o.options.audioOutFile);
  964.             if(o.options.audioMode==DSC)
  965.                 DlgSetText( hDlg, IDC_OAUDIO, "Direct stream copy");
  966.             else if(o.options.audioMode==DO_AUDIO)
  967.                 DlgSetText( hDlg, IDC_OAUDIO, "Normal audio decoding");
  968.             else if(o.options.audioMode==NO_AUDIO)
  969.                 DlgSetText( hDlg, IDC_OAUDIO, "Audio processing disabled");
  970.             if(o.options.audioMode==DO_AUDIO){
  971.                 sprintf(szTemp, "%d Hz", o.options.outSampleFrequency);
  972.                 DlgSetText( hDlg, IDC_OAUDIOFREQ,szTemp);
  973.             }
  974.             else{
  975.                 DlgSetText( hDlg, IDC_OAUDIOFREQ,"");
  976.             }
  977.             if(o.options.crop)
  978.                 DlgSetText(hDlg, IDC_OCROP, "Yes");
  979.             else
  980.                 DlgSetText(hDlg, IDC_OCROP, "No");
  981.  
  982.  
  983.           if(!o.options.compileWhole)    // Frames specified
  984.           {
  985.             porciento= ((double)o.options.presentFrame/(double)o.options.framesToCompile)*100;
  986.             SendDlgItemMessage( hDlg, IDC_PROGRESS1, PBM_SETPOS, (ui32)porciento , 0);
  987.             if( o.options.presentFrame <2 && i<200){
  988.                 i++;
  989.                 strcpy( szTemp, "Starting Up ..");
  990.                 for(j=1; j<=i; j++){
  991.                     strcat(szTemp, "..");
  992.                 }
  993.                 SetDlgItemText( hDlg, IDC_FRAMENO, szTemp);
  994.             }
  995.             else{
  996.                 sprintf(szTemp, "Processing frame %d of %d ", o.options.presentFrame, o.options.framesToCompile);
  997.                 SetDlgItemText( hDlg, IDC_FRAMENO, szTemp);
  998.             }
  999.           }
  1000.           else    //Compile whole file
  1001.           {
  1002.             porciento= ((double)(rs.video->GetVideoPos() -startPos)/(double)(rs.video->GetVideoSize()-startPos))*100;
  1003.             SendDlgItemMessage( hDlg, IDC_PROGRESS1, PBM_SETPOS, (ui32)porciento , 0);            
  1004.             if( o.options.presentFrame <2 && i<1000){
  1005.                 i++;
  1006.                 strcpy( szTemp, "Starting Up .");
  1007.                 for(j=1; j<=i; j++){
  1008.                     strcat(szTemp, ".");
  1009.                 }
  1010.                 SetDlgItemText( hDlg, IDC_FRAMENO, szTemp);
  1011.             }
  1012.             else{
  1013.                 sprintf(szTemp, "Processing frame %d", o.options.presentFrame);
  1014.                 SetDlgItemText( hDlg, IDC_FRAMENO, szTemp);
  1015.             }
  1016.  
  1017.           }
  1018.  
  1019.             sprintf(szTemp, "%d %%", (int)porciento);
  1020.             SetDlgItemText( hDlg, IDC_PERCENT, szTemp);
  1021.             //Pilla tiempo actual
  1022.             if(o.options.presentFrame == 0)
  1023.                     start_time=GetTickCount();
  1024.             present_time=GetTickCount();
  1025.             //Duracion simulacion
  1026.             MillisecondsToTime( szTemp, elapsed_time=present_time-start_time);
  1027.             SetDlgItemText( hDlg, IDC_ELAPSED, szTemp);
  1028.             // FPS
  1029.             if(elapsed_time){
  1030.                 sprintf(szTemp, "%.2f", (double)o.options.presentFrame/ ((double)elapsed_time/1000) );
  1031.                 DlgSetText( hDlg, IDC_FPS, szTemp);
  1032.             }
  1033.             //Tiempo restante
  1034.             remaining_time= (DWORD)((100-porciento)/porciento)*elapsed_time;
  1035.             MillisecondsToTime( szTemp, remaining_time);
  1036.             SetDlgItemText( hDlg, IDC_REMAINING, szTemp);
  1037.             sprintf( szTemp, "FlasK Encoding %d%% %c ", (int)porciento, chars[k++%8]);
  1038.             SetWindowText((HWND)hMainWnd, szTemp);
  1039.             //mpeg TIME
  1040.             /*sprintf( szTemp, "MPEG time: %02d:%02d:%02d:%02d", video->timeCode.hour, 
  1041.                                 video->timeCode.minute, 
  1042.                                 video->timeCode.second,
  1043.                                 video->timeCode.frame);
  1044.             SetDlgItemText( hDlg, IDC_MPEGTIME, szTemp);            */
  1045.  
  1046.  
  1047.  
  1048.         if( compileAborted /*&& audioFinished*/ )
  1049.             {
  1050.                 //Stop audio and video
  1051.                 if(rs.video)
  1052.                     rs.video->Stop();
  1053.                 if(rs.audio)
  1054.                     rs.audio->Stop();
  1055. #ifdef TESTING_MULTIAUDIO
  1056.         rs.audio2->Stop();
  1057. #endif
  1058. #ifdef TESTING_AUDIO_OUTPUT
  1059.                 fclose(test_sound_file);
  1060. #endif
  1061.  
  1062.                 PostProcessingStop(&rs.pp);
  1063.                 SetWindowText((HWND)hMainWnd, "FlasK MPEG");
  1064.                 KillTimer( hDlg, 1);
  1065.                 EnableAllMenu();
  1066.                 EndDialog(hDlg, LOWORD(wParam));
  1067.  
  1068.                 //And turndown computer if suitable
  1069.                 if(o.options.shutdown && (o.options.presentFrame >= 1000))
  1070.                     ExitWindowsEx( EWX_SHUTDOWN, 0); 
  1071.                 return TRUE;
  1072.             }
  1073.         }
  1074.         break;
  1075.         case WM_INITDIALOG:
  1076.             // Graph init
  1077.             GraphStart(&rs.gr);
  1078.  
  1079.             //Set combo box to normal and set
  1080.             SendDlgItemMessage(hDlg, IDC_PRIO, CB_ADDSTRING, 0, (LPARAM) "Idle");
  1081.             SendDlgItemMessage(hDlg, IDC_PRIO, CB_ADDSTRING, 0, (LPARAM) "Normal");
  1082.             SendDlgItemMessage(hDlg, IDC_PRIO, CB_ADDSTRING, 0, (LPARAM) "High");
  1083.             SendDlgItemMessage(hDlg, IDC_PRIO, CB_ADDSTRING, 0, (LPARAM) "Highest");
  1084.             SendDlgItemMessage(hDlg, IDC_PRIO, CB_SETCURSEL, 1, 0);
  1085.             SetPriority( NORMAL_PRIO );
  1086.  
  1087.                 if(o.options.displayVideo)
  1088.                         SendDlgItemMessage( hDlg, IDC_DISPLAYOUTPUT, BM_SETCHECK, BST_CHECKED ,0);
  1089.                 else
  1090.                         SendDlgItemMessage( hDlg, IDC_DISPLAYOUTPUT, BM_SETCHECK, BST_UNCHECKED ,0);
  1091.  
  1092.             i=0;
  1093.             compileIsPaused=false;
  1094.             
  1095.             start_time=GetTickCount();
  1096.             startPos=rs.video->GetVideoPos();
  1097.             strcpy(temp_filename, o.options.outputFile);
  1098.             add_file_ext(temp_filename);
  1099.             progressTimer= SetTimer(hDlg,              // handle of window for timer messages
  1100.                                     1,          // timer identifier
  1101.                                     500,           // time-out value
  1102.                                     NULL   // address of timer procedure
  1103.                                     );
  1104.                 
  1105.                 GetWindowRect(hDlg, &rc);
  1106.                 w = rc.right - rc.left;
  1107.                 h = rc.bottom - rc.top;
  1108.             if(rs.plugs.outPlugs[rs.selected_out_plug].fileType != VXXtype)
  1109.                 WindowResize( hDlg, w, (int)(h*0.76));
  1110.                 return TRUE;
  1111.  
  1112.         case WM_COMMAND:
  1113.             
  1114.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  1115.             {
  1116.                 abortCompile = true;
  1117.             }
  1118.             switch(LOWORD(wParam))
  1119.             {
  1120.             case IDC_PRIO:
  1121.                 if(HIWORD(wParam)==CBN_SELCHANGE)
  1122.                     SetPriority( SendDlgItemMessage(hDlg, IDC_PRIO, CB_GETCURSEL, 0,0) );
  1123.                 break;
  1124.             case IDC_PAUSE:
  1125.                 if(compileIsPaused){
  1126.                     DlgSetText(hDlg, IDC_PAUSE, "&Pause");
  1127.                     pauseCompile=false;
  1128.                     ResumeThread(hEncodingThread);
  1129.                 }
  1130.                 else{
  1131.                     DlgSetText(hDlg, IDC_PAUSE, "&Resume");
  1132.                     pauseCompile=true;
  1133.                 }
  1134.                 break;
  1135.             case IDC_DISPLAYOUTPUT:
  1136.                 if(o.options.displayVideo)
  1137.                     o.options.displayVideo=false;
  1138.                 else
  1139.                     o.options.displayVideo=true;
  1140.                 break;
  1141.             case IDC_DETAILS:
  1142.  
  1143.                 return TRUE;
  1144.             }
  1145.  
  1146.             break;
  1147.             
  1148.     }
  1149.     return FALSE;
  1150. }
  1151.  
  1152. ///////////////////////////////////
  1153. // fgetAudio : PREMIERE AUDIO STUB
  1154. ////////////////////////////////////
  1155. int fgetAudio ( long frame, long *frameCount, long *size, long offset, BufferReturnType theBuffer, long compileSeqID )
  1156. {
  1157.     static char audiodata[10240];
  1158.     static char *newblock;
  1159.  
  1160. #ifdef _DEBUG
  1161.     sprintf(szTemp, "GetAudio. Frame:%d FrameCount:%d Offset:%d ID:%d\n", frame, *frameCount, offset, compileSeqID );
  1162.     OutputDebugString( szTemp);
  1163. #endif
  1164.  
  1165.     *size = CompileInfo.outputRec.audchunksize;
  1166. #ifdef TESTING_MULTIAUDIO
  1167.     if(frame <1000000)
  1168.         audio->GetSamples( frame, (short **)&newblock ,    CompileInfo.outputRec.audchunksize>>2 );
  1169.     else
  1170.         rs.audio2->GetSamples( frame, (short **)&newblock ,    CompileInfo.outputRec.audchunksize>>2 );
  1171. #else
  1172.  
  1173.     if(!(frame <= audioPrevFrame))
  1174.         rs.audio->GetSamples( frame, (short **)&newblock ,    CompileInfo.outputRec.audchunksize>>2 );
  1175. #endif
  1176.  
  1177. #ifdef TESTING_AUDIO_OUTPUT
  1178.     if(frame <1000000)
  1179.     fwrite(newblock, *size, 1, test_sound_file);
  1180. #endif
  1181.     
  1182.     audioPrevFrame = frame;
  1183.     *theBuffer     = &newblock;
  1184.  
  1185.     return comp_ErrNone;
  1186. }
  1187.  
  1188. long fgetBlipMax ( long compileSeqID ) 
  1189. {
  1190.     printf("GetBlipMax. ID:%d\n",compileSeqID);
  1191.     return CompileInfo.outputRec.audchunksize;
  1192. }
  1193. ///////////////////////////////////
  1194. // fgetFrame : PREMIERE VIDEO STUB
  1195. ////////////////////////////////////
  1196. int fgetFrame ( long frame, void **buffer, long *rowbytes, compGetFrameReturnRec *getFrameReturn, char getCompressed, long compileSeqID )
  1197. {
  1198.     static compGetFrameReturnRec ReturnRecord;
  1199.     char szTemp[260];
  1200.     static HANDLE hDC;
  1201.  
  1202.     YUVImageTag  *yuv_frame=NULL;
  1203.  
  1204.     if(rs.plugs.outPlugs[rs.selected_out_plug].fileType == VXXtype)
  1205.         UpdateGraph(&rs.gr, (TAVIPlugGraphInfo *)compileSeqID);
  1206.     
  1207.     if(firstTime){
  1208.         myClock   = 0;
  1209.         firstTime = false;
  1210.     }
  1211.     if(!firstImageAlreadyDecoded){
  1212.         while(1){
  1213.             if(!imageWaiting){
  1214.                 if(!rs.video->GetFrame(&pInfo, &yuv_frame)){
  1215.                     // If bbMPEG is being used
  1216.                     //    don't abort
  1217.                     if(rs.plugs.outPlugs[rs.selected_out_plug].fileType!='\0MPG')
  1218.                         abortCompile=true;
  1219.                     break;
  1220.                 }
  1221. /*                if(!pInfo.SCRlocked || firstTime){//Update clocks
  1222.                     myClock=pInfo.imagePTS- (frameSpan/2);
  1223.                     firstTime=false;
  1224.                 }*/
  1225.             }
  1226.             //Check if th image has to be displayed in this frame
  1227.             if((pInfo.imagePTS >= myClock) && (pInfo.imagePTS<(myClock+frameSpan))){
  1228.                 imageWaiting=false;
  1229.                 break;
  1230.             }
  1231.             else if( pInfo.imagePTS >= (myClock+frameSpan) ){
  1232.                     imageWaiting=true;
  1233.                     break;
  1234.             }
  1235.             //Image has a PTS lower than myClock. Drop it
  1236.             imageWaiting=false;
  1237.         }
  1238.     }
  1239.     else{//FirstImageAlreadyDecoded
  1240.         firstImageAlreadyDecoded=false;
  1241.     }
  1242.         myClock+=frameSpan;
  1243.         //If GetFrame returns 0, video bitstream is over
  1244.  
  1245.         if(rs.audio)
  1246.             if(o.options.audioMode==DSC)//If we are in Direct Stream Copy audio mode
  1247.                 rs.audio->GetAudioDSC(pInfo.imagePTS, rs.video->GetVideoPos());
  1248.  
  1249.         YUVtoRGB(yuv_frame, MPEGImage);
  1250.  
  1251.         rs.pp.myClock = myClock;
  1252.         PostProcess(&DecodedImage, &Display, &rs.pp);
  1253.  
  1254.         if(o.options.displayVideo)
  1255.         {
  1256.             hDC = GetDC(hMainWnd);
  1257.             SetDIBitsToDevice((HDC) hDC,0,0,PPOST_WIDTH(rs.pp),PPOST_HEIGHT(rs.pp),
  1258.                      0,0,0,PPOST_HEIGHT(rs.pp), (ui8 *)Display.Address(),&DibInfo,DIB_RGB_COLORS);
  1259.             ReleaseDC( hMainWnd, (HDC)hDC);
  1260.         }
  1261.     
  1262.         if(pauseCompile){
  1263.             compileIsPaused=true;
  1264.             SuspendThread(hEncodingThread);
  1265.             compileIsPaused=false;
  1266.         }
  1267.         
  1268.     *buffer   = Display.Address();
  1269.     *rowbytes = 4*PPOST_WIDTH(rs.pp);
  1270.  
  1271.     ReturnRecord.returnVal = comp_ErrNone;
  1272.     ReturnRecord.repeatCount = 0;
  1273.     ReturnRecord.makeKeyFrame = FALSE;
  1274.     ReturnRecord.frameIsCompressed = FALSE;
  1275.     ReturnRecord.startframe = frame;
  1276.     ReturnRecord.frameOnMarker = FALSE;
  1277.  
  1278.  
  1279.     o.options.presentFrame++;
  1280.     sprintf(szTemp, "GetFrame. Frame:%d ID:%d\n", frame, compileSeqID );
  1281.  
  1282.     if(abortCompile || o.options.presentFrame > o.options.framesToCompile )
  1283.         ReturnRecord.returnVal = comp_CompileAbort;
  1284.         
  1285.     *getFrameReturn = ReturnRecord;
  1286.     return ReturnRecord.returnVal;
  1287. }
  1288.  
  1289.  
  1290. DWORD WINAPI  EncodingThread(LPVOID *nada)
  1291. {
  1292.     static HINSTANCE            hinstLib,AVIplugLib,PANplugLib; 
  1293.     BOOL fRunTimeLinkSuccess = FALSE;
  1294.     int round;
  1295.  
  1296.     CompileEntryFunc CompileEntry;
  1297.  
  1298.     hinstLib   = NULL;
  1299.     PANplugLib = NULL;
  1300.  
  1301.  
  1302.     while(1)
  1303.     {
  1304.         switch(sendEncodingThread){
  1305.         case UNLOAD_LIBRARY:
  1306.             break;
  1307.         case LOAD_LIBRARY:
  1308.  
  1309.             break;
  1310.         case ENCODE:
  1311.  
  1312.                     o.options.presentFrame=0;
  1313.                     //Setup compiler plugin info
  1314.                     CompileInfo.compilerID=rs.plugs.outPlugs[rs.selected_out_plug].CompilerID;
  1315.                     CompileInfo.compilerPrefs=rs.plugs.outPlugs[rs.selected_out_plug].settings;
  1316.                         //CompOutputRec
  1317.                         CompileInfo.outputRec.doVideo=1;
  1318.                         CompileInfo.outputRec.doAudio= (o.options.audioMode==DO_AUDIO);
  1319.                             //vidCompression
  1320.                             CompileInfo.outputRec.vidCompression.subtype  = rs.plugs.outPlugs[rs.selected_out_plug].fileType;
  1321.                             CompileInfo.outputRec.vidCompression.depth    = 16;
  1322.                             CompileInfo.outputRec.vidCompression.recompressWhen=recompNever;
  1323.  
  1324.                             //CompileInfo.subtype=un
  1325.                             //PostProcessing int gammaCorrection;
  1326.                             CompileInfo.postProcessing.gammaCorrection=1;
  1327.                         CompileInfo.outputRec.width=   PPOST_WIDTH(rs.pp) ;
  1328.                         CompileInfo.outputRec.height=  PPOST_HEIGHT(rs.pp);
  1329.                             //compTimebaseRec
  1330.                             CompileInfo.outputRec.timebase.scale=            o.options.timeBase.scale;
  1331.                             CompileInfo.outputRec.timebase.sampleSize=        o.options.timeBase.sampleSize;
  1332.                             CompileInfo.outputRec.timebase.value=            o.options.timeBase.value;
  1333.                         CompileInfo.outputRec.fieldType=     compFieldsNone;
  1334.                     
  1335.                     //compPostProcessing
  1336.                     //compFileSpec
  1337.                         strcpy( CompileInfo.outputFile.name, o.options.outputFile );
  1338.                         add_file_ext(CompileInfo.outputFile.name);
  1339.                         //Now we create the options file     
  1340.                         /*options.hOutputFile = CreateFile(options.outputFile,  // create file
  1341.                                                  GENERIC_WRITE,// open for writing 
  1342.                                                  0,            // do not share 
  1343.                                                   NULL,         // no security 
  1344.                                                  CREATE_ALWAYS,// overwrite existing 
  1345.                                                  FILE_ATTRIBUTE_NORMAL |// normal file 
  1346.                                                  FILE_FLAG_OVERLAPPED,  // asynchronous I/O 
  1347.                                                  NULL);// no attr. template 
  1348.     
  1349.                         if (options.hOutputFile == INVALID_HANDLE_VALUE) 
  1350.                         { 
  1351.                             //ErrorHandler("Could not open file.");  // process error 
  1352.                             break;
  1353.                         }*/    
  1354.                 WAVEFORMATEX AudioCompressionRecord;
  1355.  
  1356.                 AudioCompressionRecord.wFormatTag = WAVE_FORMAT_PCM;
  1357.                 AudioCompressionRecord.nChannels = 2;
  1358.                 AudioCompressionRecord.nSamplesPerSec = o.options.outSampleFrequency;
  1359.                 AudioCompressionRecord.nAvgBytesPerSec = o.options.outSampleFrequency*4;
  1360.                 AudioCompressionRecord.nBlockAlign = 4;
  1361.                 AudioCompressionRecord.wBitsPerSample = 16; 
  1362.                 AudioCompressionRecord.cbSize = 0;
  1363.                 
  1364.                 memcpy(&CompileInfo.outputRec.audCompression.AudCompRec, (char *)&AudioCompressionRecord, 30);
  1365.                 CompileInfo.outputRec.audCompression.subtype = 0;
  1366.  
  1367.                 CompileInfo.outputRec.fieldType = compFieldsNone;
  1368.                 CompileInfo.outputRec.audrate = o.options.outSampleFrequency;
  1369.                 CompileInfo.outputRec.audsamplesize = 16;
  1370.                 CompileInfo.outputRec.stereo = TRUE;
  1371.                 CompileInfo.outputRec.audchunksize = (int)((CompileInfo.outputRec.audrate)/((double)o.options.timeBase.scale/(double)o.options.timeBase.sampleSize)*4); //samples/Frame * 2bytes/sample * 2channels 
  1372.                 //round chunksize to the next 4 boundary. A stereo sample takes 4 bytes
  1373.                 CompileInfo.outputRec.audchunksize += (round=CompileInfo.outputRec.audchunksize % 4) ? (4-round): round;
  1374.  
  1375.  
  1376.                 CompileInfo.outputRec.audInterleave = prInterleave1Frame;
  1377.  
  1378.                         o.options.hOutputFile=NULL;
  1379.                         //To close: CloseHandle
  1380.                         CompileInfo.outputFileRef= (compFileRef) o.options.hOutputFile;
  1381.                         CompileInfo.startFrame=    1;
  1382.                         CompileInfo.endFrame=    o.options.framesToCompile;
  1383.                         CompileInfo.compileSeqID=  0;
  1384.                                 // Awake decoding process
  1385.                                     //Decoding process parameters
  1386.  
  1387.                 
  1388.                                 decodingFinished=TRUE;
  1389.  
  1390.                         abortCompile=false;
  1391.                         compileAborted=false;
  1392.  
  1393.  
  1394.                 //Start compile
  1395.                         CompileEntry = rs.plugs.outPlugs[rs.selected_out_plug].CompileEntry;
  1396.                         if(CompileEntry)
  1397.                             (CompileEntry)(compDoCompile, &stdParms, (long)&CompileInfo,0);
  1398.                         // If my decoding process was sleep awake it and inform it to abort
  1399.                         abortCompile=true;
  1400.                         SetWindowSize();
  1401.                         InvalidateRect(hMainWnd, NULL, true);
  1402.  
  1403.  
  1404.                         compileAborted=true;
  1405.  
  1406.                         
  1407.             break;
  1408.         }
  1409.         SuspendThread(hEncodingThread);
  1410.  
  1411.  
  1412.     }
  1413.     return 1;
  1414. }
  1415.  
  1416.  
  1417. void EncoderMessage( int message )
  1418. {
  1419.     sendEncodingThread= (EncEnum) message;
  1420.     ResumeThread(hEncodingThread);
  1421.   
  1422. };
  1423.  
  1424.  
  1425. ///////////////////////////////////////////////////////
  1426. //        PLAYER
  1427. ///////////////////////////////////////////////////////
  1428.  
  1429. HWND   hPlayerDlg;
  1430. HANDLE evPlayerStopped; 
  1431. HANDLE evFrameCaptured;
  1432. CRITICAL_SECTION GlobalCriticalSection; 
  1433. bool   SeekInProgress;
  1434. i64        diferencia;
  1435.  
  1436.  
  1437.  
  1438. DWORD WINAPI  PlayerThread(LPVOID *nada){
  1439.  
  1440. HDC hDC;
  1441. static unsigned char newblock[MAX_IMAGE_MEM];
  1442.     static i64 prevTime;
  1443.     static presInfo pInfo;
  1444.     YUVImageTag  *frame;
  1445.  
  1446.     while(1)
  1447.     {
  1448.         if(!rs.video->GetFrame(&pInfo, &frame)){
  1449.                 switch(rs.video->GetError())
  1450.                 {
  1451.                 case END_OF_STREAM:
  1452.                         //Stream ended. Push stop button
  1453.                         SendMessage(hPlayerDlg, WM_COMMAND, MAKEWPARAM(IDC_STOP,0), 0);
  1454.                         SetEvent(evPlayerStopped);
  1455.                         return 0;
  1456.                     break;
  1457.                 case PLAYER_STOPPED:
  1458.                         SetEvent(evPlayerStopped);
  1459.                         return 0;
  1460.                     break;
  1461.                 }
  1462.  
  1463.         }
  1464.         diferencia=pInfo.imagePTS-prevTime;
  1465.         prevTime=pInfo.imagePTS;
  1466.  
  1467.         rs.audio->sysClock=pInfo.imagePTS;
  1468.         if(rs.audio)
  1469.             if(o.options.audioMode==DSC)//If we are in Direct Stream Copy audio mode
  1470.                 rs.audio->GetAudioDSC(pInfo.imagePTS, rs.video->GetVideoPos());
  1471.  
  1472.         SetEvent(evFrameCaptured);
  1473.         YUVtoRGB(frame, MPEGImage);
  1474.  
  1475.         myClock+=frameSpan;
  1476.         rs.pp.myClock = myClock;
  1477.         PostProcess(&DecodedImage, &Display, &rs.pp);
  1478.  
  1479.  
  1480.         hDC = GetDC(hMainWnd);
  1481.         SetDIBitsToDevice((HDC) hDC,0,0,PPOST_WIDTH(rs.pp),PPOST_HEIGHT(rs.pp),
  1482.                      0,0,0,PPOST_HEIGHT(rs.pp), (ui8 *)Display.Address(),&DibInfo,DIB_RGB_COLORS);
  1483.         ReleaseDC( hMainWnd, (HDC)hDC);
  1484.     }
  1485.  
  1486. }
  1487. LRESULT CALLBACK PlayerDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  1488. {
  1489.     static HANDLE hPlayer;
  1490.     DWORD PlayerIDThread;
  1491.     static playerTimer;
  1492.     char szTemp[256];
  1493.     int fPosition, lPosition;
  1494.     static bool userIsTracking;
  1495.     TVideoOptions video_opt;
  1496.     static int kkk;
  1497.     bool doNothing;
  1498.  
  1499.  
  1500.     switch (message)
  1501.     {
  1502.         case WM_TIMER:
  1503.             // time
  1504.             MillisecondsToTime(szTemp, rs.video->time);
  1505.             SetDlgItemText(hDlg, IDC_PLAYTIME, szTemp);
  1506.             // Total frames
  1507.             // File
  1508.             DlgSetText(hDlg, IDC_FILE, rs.video->GetVideoFileName());
  1509.             // Total file size
  1510.             sprintf( szTemp, "%d Mbytes", rs.video->GetVideoSize()/(1024*1024));
  1511.             SetDlgItemText(hDlg, IDC_FILESIZE, szTemp);
  1512.             // Video size
  1513.             DlgSetText(hDlg,IDC_VIDEOSIZE ,rs.video->sVideoSize);
  1514.             // Frame rate
  1515.             DlgSetText(hDlg,IDC_FRAMERATE ,rs.video->sFrameRate);
  1516.             //Detected FPS
  1517.             DlgSetText(hDlg,IDC_DETECTEDFPS ,rs.video->sDetectedFPS);
  1518.             // Aspect Ratio
  1519.             DlgSetText(hDlg,IDC_ASPECTRATIO ,rs.video->sAspectRatio);
  1520.             // Bitrate
  1521.             DlgSetText(hDlg,IDC_BITRATE ,rs.video->sBitrate);
  1522.             // Progressive sequence
  1523.             DlgSetText(hDlg,IDC_PROGRESSIVE ,rs.video->sProgressive);
  1524.             //DlgSetText(hDlg,IDC_PROGRESSIVE ,video->internalPTS/27000.0);
  1525.             //DlgSetText(hDlg,IDC_BITRATE ,diferencia);
  1526.  
  1527.             fPosition=true;
  1528.             lPosition= (int)(((double)(rs.video->GetVideoPos())/(double)(rs.video->GetVideoSize()))*1000);
  1529.             if( !userIsTracking && !SeekInProgress )
  1530.                 SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETPOS, (WPARAM) (BOOL) fPosition,(LPARAM) (LONG) lPosition); 
  1531.  
  1532.             break;
  1533.         case WM_INITDIALOG:
  1534.             myClock=0;
  1535.             frameSpan= (i64)((1/((double)(o.options.timeBase.scale)/
  1536.                      (double)(o.options.timeBase.sampleSize)))*(double)MPEG2_CLK_REF);
  1537.             // Seek video
  1538.             rs.video->SeekVideo(rs.startFilePos);
  1539.             if(rs.audio)
  1540.                 rs.audio->SeekAudio(rs.startFilePos);
  1541.  
  1542.             // Initialize the critical section.
  1543.             InitializeCriticalSection(&GlobalCriticalSection);
  1544.             
  1545.             // Get post processing options from config
  1546.             GetPPostConfig(&o, &rs.pp);
  1547.             // Set additional parameters
  1548.             rs.pp.iDAR = rs.video->DAR;
  1549.             InitDibDisplay(PPOST_WIDTH(rs.pp) , PPOST_HEIGHT(rs.pp));
  1550.  
  1551.             DecodedImage.init(&MPEGImage, rs.video->pictureWidth, rs.video->picutreHeight, 32);
  1552.             DecodedImage.AlignTo4();
  1553.             // Start Post processing
  1554.             PostProcessingStart(&DecodedImage, &Display, &rs.pp);
  1555.  
  1556.             
  1557.  
  1558.             hPlayerDlg=hDlg;
  1559.             SeekInProgress=false;
  1560.             userIsTracking=false;
  1561.                         EnableWindow( GetDlgItem(hDlg, IDC_PLAY), true); 
  1562.                         EnableWindow( GetDlgItem(hDlg, IDOK), true); 
  1563.                         EnableWindow( GetDlgItem(hDlg, IDC_SEEKFIRST), true); 
  1564.                         EnableWindow( GetDlgItem(hDlg, IDC_SLIDER), false); 
  1565.                         EnableWindow( GetDlgItem(hDlg, IDC_STOP), false); 
  1566.             SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETRANGE, (WPARAM) TRUE,(LPARAM) MAKELONG(0, 1000)); 
  1567.             
  1568.             evPlayerStopped=CreateEvent(NULL, TRUE, FALSE,NULL);
  1569.             evFrameCaptured=CreateEvent(NULL, TRUE, FALSE,NULL);
  1570.             
  1571.  
  1572.             playerTimer= SetTimer(hDlg,              // handle of window for timer messages
  1573.                                      2,          // timer identifier
  1574.                                    500,           // time-out value
  1575.                                    NULL   // address of timer procedure
  1576.                                    );
  1577.                 return TRUE;
  1578.         case WM_HSCROLL:
  1579.             int nPos;
  1580.             switch(LOWORD(wParam)){
  1581.                 case TB_THUMBTRACK:
  1582.                     userIsTracking = true;
  1583.                     break;
  1584.  
  1585.                 case TB_THUMBPOSITION :
  1586.  
  1587.                 //Disable trackbar
  1588.                 EnterCriticalSection(&GlobalCriticalSection);
  1589.                     if(SeekInProgress)
  1590.                         doNothing=true;
  1591.                     else{
  1592.  
  1593.                         doNothing=false;
  1594.                     }
  1595.                 LeaveCriticalSection(&GlobalCriticalSection);
  1596.                 if(doNothing) return 0;
  1597.                 
  1598.                 SeekInProgress=true;
  1599.                 DlgDisable(hDlg, IDC_SLIDER);
  1600.                 nPos = SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_GETPOS, 0, 0);   
  1601.                 //Stopvideo
  1602.                     ResetEvent(evPlayerStopped);
  1603.                     rs.video->stopDecoding=true;
  1604.                     WaitForSingleObject(evPlayerStopped, 5000);
  1605.                     CloseHandle(hPlayer);
  1606.                     rs.video->Stop();
  1607.                     if(rs.audio)
  1608.                         if(o.options.audioMode==DSC)
  1609.                             rs.audio->Stop();
  1610.                 //Seek
  1611.                     rs.startFilePos=(i64)(((double)nPos/1000.0)*(double)(rs.video->GetVideoSize()));
  1612.                     rs.video->SeekVideo(rs.startFilePos);
  1613.                     if(rs.audio)
  1614.                         rs.audio->SeekAudio(rs.startFilePos);
  1615.                 //
  1616.                 video_opt.idctType             = o.options.idctType;
  1617.                 video_opt.recons_progressive   = o.options.recons_progressive;
  1618.                 myClock=0;
  1619.                 rs.video->Start(&video_opt);
  1620.                 if(rs.audio)
  1621.                     if(o.options.audioMode==DSC)
  1622.                         rs.audio->Start(o.options.audioOutFile, o.options.audioMode);
  1623.  
  1624.                 hPlayer=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) PlayerThread,(LPVOID)NULL,0,&PlayerIDThread);  
  1625.                 ResetEvent(evFrameCaptured);
  1626.                 WaitForSingleObject(evFrameCaptured, 5000);
  1627.                 userIsTracking=false;
  1628.                 SeekInProgress=false;        //if we were seeking
  1629.                 EnableWindow( GetDlgItem(hDlg, IDC_SLIDER), true); 
  1630.                 break;
  1631.             }
  1632.             break;
  1633.         case WM_COMMAND:
  1634.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  1635.             {
  1636.                 CloseHandle(evPlayerStopped);
  1637.                 CloseHandle(evFrameCaptured);
  1638.  
  1639.  
  1640.                 PostProcessingStop(&rs.pp);
  1641.                 KillTimer( hDlg, 2);
  1642.                 SetWindowSize();
  1643.                 InvalidateRect(hMainWnd, NULL, true);
  1644.                 EndDialog(hDlg, LOWORD(wParam));
  1645.                 return TRUE;
  1646.             }
  1647.             switch( LOWORD(wParam)){
  1648.                 case IDC_SEEKFIRST:
  1649.                     rs.startFilePos=0;
  1650.                     rs.video->SeekVideo(rs.startFilePos);
  1651.                     if(rs.audio)
  1652.                         rs.audio->SeekAudio(rs.startFilePos);
  1653.                 break;
  1654.                 case IDC_PLAY:
  1655.                         playerStopped=false;
  1656.                         //Grey PLAY AND EXIT
  1657.                         EnableWindow( GetDlgItem(hDlg, IDC_PLAY), false); 
  1658.                         EnableWindow( GetDlgItem(hDlg, IDOK), false); 
  1659.                         EnableWindow( GetDlgItem(hDlg, IDC_SEEKFIRST), false); 
  1660.                         EnableWindow( GetDlgItem(hDlg, IDC_SLIDER), true); 
  1661.                         EnableWindow( GetDlgItem(hDlg, IDC_STOP), true); 
  1662.  
  1663.                         video_opt.idctType             = o.options.idctType;
  1664.                         video_opt.recons_progressive   = o.options.recons_progressive;
  1665.                         myClock=0;
  1666.                         rs.video->Start(&video_opt);
  1667.                         if(rs.audio)
  1668.                             if(o.options.audioMode==DSC)
  1669.                                 rs.audio->Start(o.options.audioOutFile, o.options.audioMode);
  1670.                         
  1671.                         
  1672.                         hPlayer=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) PlayerThread,(LPVOID)NULL,0,&PlayerIDThread);  
  1673.                     break;
  1674.                 case IDC_STOP:
  1675.  
  1676.                         EnableWindow( GetDlgItem(hDlg, IDC_SLIDER), false); 
  1677.                         EnableWindow( GetDlgItem(hDlg, IDC_STOP), false); 
  1678.                         
  1679.                         ResetEvent(evPlayerStopped);
  1680.                         rs.video->stopDecoding=true;
  1681.                         WaitForSingleObject(evPlayerStopped, 5000);
  1682.                         
  1683.                         CloseHandle(hPlayer);
  1684.                         rs.video->Stop();
  1685.                         if(rs.audio)
  1686.                             if(o.options.audioMode==DSC)
  1687.                                 rs.audio->Stop();
  1688.                         EnableWindow( GetDlgItem(hDlg, IDC_PLAY), true); 
  1689.                         EnableWindow( GetDlgItem(hDlg, IDOK), true);
  1690.                         EnableWindow( GetDlgItem(hDlg, IDC_SEEKFIRST), true); 
  1691.                         playerStopped=true;
  1692.                     break;
  1693.  
  1694.             }
  1695.             break;
  1696.     }
  1697.     return FALSE;
  1698. }
  1699.