home *** CD-ROM | disk | FTP | other *** search
- /*
- * FlasKMPEG.cpp
- *
- * Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
- *
- * This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
- *
- * FlasKMPEG is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- *
- * FlasKMPEG is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with GNU Make; see the file COPYING. If not, write to
- * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- */
-
-
-
- /*extern "C"{*/
- #include "FlasKMPEG.h"
- /*}*/
- #include ".\Demux\Demux.h"
- #include ".\Video\VideoWrapper.h"
- #include ".\Audio\Audio.h"
- #include ".\Input\StreamDetector.h"
- #include ".\Input\DVDSelector.h"
- #include ".\plugins.h"
- #include ".\error.h"
- #include ".\RunState.h"
- #include ".\language.h"
- #include ".\Misc\Graph.h"
- #include ".\FileOpen.h"
- #include ".\Audio\AudioPlayback.h"
-
- #include "YUVtoRGB.h"
- #include <malloc.h>
- #include <commdlg.h>
- #include <CommCtrl.h>
- #include "resource.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <fcntl.h>
- #include <math.h>
-
- #include ".\Resizer\filter.h"
- #include ".\Resizer\resizer.h"
- #include ".\postprocessing.h"
- #include ".\windebug.h"
- #include "auxiliary.h"
- #include "mmx.h"
-
- #define MAX_LOADSTRING 100
- #define TESTING_IFO_PARSER
- //#define TESTING_AUDIO_OUTPUT
- //#define TESTING_MULTIAUDIO
- // Global Variables:
- extern "C" BOOL FPU_enabled;
-
- BOOL FPU_enabled;
- HINSTANCE hInst; // current instance
- HWND hConsole;
- TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
- TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
- HANDLE hThrd;
- DWORD IDThread, ENCIDThread;
- HANDLE hEncodingThread;
- UINT progressTimer;
-
-
- unsigned char *newblock;
-
- TRunState rs;
-
- VBitmap Display;
- VBitmap DecodedImage;
-
- HWND hMainWnd;
- BITMAPINFOHEADER DibHeader;
- BITMAPINFO DibInfo;
- BOOL playerStopped;
- BOOL abortCompile; //Use this to abort the compile
- BOOL audioFinished;
- BOOL compileAborted; //Use this to kill progress dialog
- BOOL decodingFinished; //The decoding thread has finished
- //framerate sync variables
- i64 myClock;
- static i64 frameSpan;
- bool imageWaiting;
- bool firstTime;
- bool firstImageAlreadyDecoded=false;
- presInfo pInfo;
- // Compiling globals
- int audioPrevFrame;
- bool pauseCompile;
- bool compileIsPaused;
-
- TFileProgress FileProgress;
-
- extern void InitPremiereCallbacks();
- extern compStdParms stdParms;
-
- typedef enum{LOAD_LIBRARY,
- ENCODE,
- UNLOAD_LIBRARY} EncEnum;
- EncEnum sendEncodingThread;
-
- unsigned char DisplayImage[MAX_IMAGE_MEM];
- unsigned char MPEGImage[MAX_IMAGE_MEM];
-
- char *chars="-\\|/-\\|/";
-
- //
- #ifdef TESTING_AUDIO_OUTPUT
- FILE *test_sound_file;
- #endif
- // Output Options
- TConfig o;
-
-
-
-
-
-
- // Premiere Related Globals
- compGetFilePrefsRec FilePrefsRec; //For private settings of plugin
- compDoCompileInfo CompileInfo; //For Compiling...
-
-
-
- // Foward declarations of functions included in this code module:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK OutputOptionsDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
- LRESULT CALLBACK ProgressDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
- LRESULT CALLBACK DlgConsole(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
- LRESULT CALLBACK PlayerDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
- void SetTextMenu();
-
- void EncoderMessage( int message );
- DWORD WINAPI EncodingThread(LPVOID *nada);
-
-
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- // TODO: Place code here.
- MSG msg;
- HACCEL hAccelTable;
-
- rs.lng = new CLanguage;
- if(rs.lng){
- rs.lng->Start();
- }
-
- // Initialize global strings
- strcpy( szTitle, GS(APPTITLE) );
- // LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_FLASKMPG2MPG1, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
-
- // Perform application initialization:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
-
- hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_FLASKMPG2MPG1);
-
- // Main message loop:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
-
- return msg.wParam;
- }
-
-
- // BITMAP DISPLAY INIT
- void InitDibDisplay(int oxsize, int oysize){
- RECT wr,cr;
- int d;
- Display.init(DisplayImage, oxsize, oysize, 32);
- Display.MakeBitmapHeader(&DibHeader);
-
- memcpy(&DibInfo.bmiHeader, &DibHeader, sizeof(BITMAPINFOHEADER));
- // Set Window Size
-
- // size window to film size
- SetRect(&cr,0,0,oxsize, oysize);
- wr=cr;
- AdjustWindowRect(&wr,GetWindowLong(hMainWnd,GWL_STYLE),TRUE);
- OffsetRect(&wr,-wr.left,-wr.top);
- SetWindowPos(hMainWnd,HWND_TOP,0,0,wr.right,wr.bottom,SWP_NOMOVE);
-
- // for QSIF movies the menu will wrap around so we need
- // to increase the height of the window
- GetClientRect(hMainWnd, &wr);
- d=cr.bottom-(wr.bottom-wr.top);
-
- if (d>0)
- {
- GetWindowRect(hMainWnd, &wr);
- SetWindowPos(hMainWnd,HWND_TOP,0,0,wr.right-wr.left,wr.bottom-wr.top+d,SWP_NOMOVE);
- }
-
- }
-
- char * add_file_ext(char *file){
- if(!file)
- return NULL;
- if(rs.plugs.outPlugs[rs.selected_out_plug].fileType==VXXtype)
- strcat(file,".avi");
- if(rs.plugs.outPlugs[rs.selected_out_plug].fileType=='MPEG'){
- if(o.options.audioMode==DO_AUDIO)
- strcat(file,".mpg");
- else
- strcat(file,".m1v");
- }
- if(rs.plugs.outPlugs[rs.selected_out_plug].fileType=='\0MPG'){
- if(o.options.audioMode==DO_AUDIO)
- strcat(file,".mpg");
- else
- strcat(file,".m1v");
- }
- return file;
- }
- void SetWindowSize( void )
- {
- RECT cr,wr;
- int d;
- // size window to film size
- SetRect(&cr,0,0,352, 288);
- wr=cr;
- AdjustWindowRect(&wr,GetWindowLong((HWND)hMainWnd,GWL_STYLE),TRUE);
- OffsetRect(&wr,-wr.left,-wr.top);
- SetWindowPos((HWND)hMainWnd,HWND_TOP,0,0,wr.right,wr.bottom,SWP_NOMOVE);
-
- // for QSIF movies the menu will wrap around so we need
- // to increase the height of the window
- GetClientRect((HWND)hMainWnd, &wr);
- d=cr.bottom-(wr.bottom-wr.top);
-
- if (d>0)
- {
- GetWindowRect((HWND)hMainWnd, &wr);
- SetWindowPos((HWND)hMainWnd,HWND_TOP,0,0,wr.right-wr.left,wr.bottom-wr.top+d,SWP_NOMOVE);
- }
- }
-
-
-
- //
- // FUNCTION: MyRegisterClass()
- //
- // PURPOSE: Registers the window class.
- //
- // COMMENTS:
- //
- // This function and its usage is only necessary if you want this code
- // to be compatible with Win32 systems prior to the 'RegisterClassEx'
- // function that was added to Windows 95. It is important to call this function
- // so that the application will get 'well formed' small icons associated
- // with it.
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
-
- wcex.cbSize = sizeof(WNDCLASSEX);
-
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = (HICON)LoadImage(hInstance, (LPCTSTR)IDI_FLASKMPG2MPG1, IMAGE_ICON, 0,0,0);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
- wcex.lpszMenuName = (LPCSTR)IDC_FLASKMPG2MPG1;
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = NULL;//LoadIcon(wcex.hInstance, (LPCTSTR)IDI_FLASKMPG2MPG1);
-
- return RegisterClassEx(&wcex);
- }
-
- //
- // FUNCTION: InitInstance(HANDLE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- // Mesage handler for about box.
- LRESULT CALLBACK WelcomeDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int n_lang, i;
- switch (message)
- {
- case WM_INITDIALOG:
- //Language Selection
- DlgSetText(hDlg, R_WELCOME_LANG, GS(MENU_LANGUAGE));
- n_lang = rs.lng->GetNumberLang();
- for(i=0; i<n_lang; i++){
- rs.lng->SetLanguage(i);
- ListAddText(GetDlgItem(hDlg, R_LANG_LIST), rs.lng->GetLanguageID() );
- }
- ListSetCur(GetDlgItem(hDlg, R_LANG_LIST), 0);
- return TRUE;
-
- case WM_COMMAND:
-
-
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- if(HIWORD(wParam)==CBN_SELCHANGE){
- rs.lng->SetLanguage( SendDlgItemMessage(hDlg, R_LANG_LIST, CB_GETCURSEL, 0,0) );
- o.options.selected_language = rs.lng->GetLanguage();
- SetTextMenu();
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
-
- void SetTextMenu(){
- HMENU menu;
- int i;
-
- MenuSetText(GetMenu(hMainWnd), 0, GS(MENU_FILE));
- MenuSetText(GetMenu(hMainWnd), 1, GS(MENU_OPTIONS));
- MenuSetText(GetMenu(hMainWnd), 2, GS(MENU_RUN));
- MenuSetText(GetMenu(hMainWnd), 3, GS(MENU_ABOUT));
-
- // File preferences
- menu = MenuGetPopup(GetMenu(hMainWnd), 0);
- MenuSetText( menu, 0, GS(MENU_OPEN));
- MenuSetText( menu, 1, GS(MENU_OPENDVDFILE));
- MenuSetText( menu, 3, GS(MENU_EXIT));
-
- // Options preferences
- menu = MenuGetPopup(GetMenu(hMainWnd), 1);
- MenuSetText( menu, 0, GS(MENU_OUTPUTFORMAT));
- MenuSetText( menu, 1, GS(MENU_OUTOPTIONS));
- MenuSetText( menu, 3, GS(MENU_SELECTOUT));
- MenuSetText( menu, 5, GS(MENU_LANGUAGE));
-
- // Output plugins
- menu = MenuGetPopup(GetMenu(hMainWnd), 1);
- menu = MenuGetPopup(menu, 3);
- while(RemoveMenu(menu, 0, MF_BYPOSITION ));
-
- for(i=0; i<rs.plugs.OutPluginCount; i++){
- MenuAddItem( menu, i, i+10300, rs.plugs.outPlugs[i].ID );
- }
- MenuCheck(hMainWnd, 10300+rs.selected_out_plug);
-
- //Languages menus
- // Get languages popup
- menu = MenuGetPopup(GetMenu(hMainWnd), 1);
- menu = MenuGetPopup(menu, 5);
-
- while(RemoveMenu(menu, 0, MF_BYPOSITION ));
-
-
- int current_lang = rs.lng->GetLanguage();
- int n_lang = rs.lng->GetNumberLang();
- for(i=0; i<n_lang; i++){
- rs.lng->SetLanguage(i);
- MenuAddItem(menu, i, i+10000, rs.lng->GetLanguageID() );
- }
-
- rs.lng->SetLanguage(current_lang);
-
- //Run
- menu = MenuGetPopup(GetMenu(hMainWnd), 2);
- MenuSetText( menu, 0, GS(MENU_PLAYER));
- MenuSetText( menu, 1, GS(MENU_STARTCONV));
-
-
- DrawMenuBar( hMainWnd );
-
-
- }
- void SetMenuMPEG(){
- MenuCheck(hMainWnd, IDM_MPEG);
- MenuUnCheck(hMainWnd, IDM_AVI);
- MenuEnable(hMainWnd,IDM_MPEG1OPTIONS);
- }
- void SetMenuAVI(){
- MenuCheck(hMainWnd, IDM_AVI);
- MenuUnCheck(hMainWnd, IDM_MPEG);
- //MenuDisable(hMainWnd,IDM_MPEG1OPTIONS);
- MenuEnable(hMainWnd,IDM_MPEG1OPTIONS);
- }
- void EnableMenu(){
- MenuEnable(hMainWnd, IDM_PLAY);
- MenuEnable(hMainWnd, IDM_MPEG1OPTIONS);
-
- MenuEnable(hMainWnd, IDM_START2);
- MenuEnable(hMainWnd, IDM_OUTPUTCONFIG);
- //EnableMenuItem(GetSubMenu(GetMenu(hMainWnd), 1), GetMenuItemID(GetSubMenu(GetMenu(hMainWnd), 1),3), MF_GRAYED);
-
- }
- void DisableMenu(){
- MenuDisable(hMainWnd, IDM_PLAY);
- MenuDisable(hMainWnd, IDM_MPEG1OPTIONS);
- MenuDisable(hMainWnd, IDM_START2);
- MenuDisable(hMainWnd, IDM_OUTPUTCONFIG);
- // MenuDisable(hMainWnd, IDM_DISPLAYVIDEO);
- MenuDisable(hMainWnd, IDM_MPEG);
- MenuDisable(hMainWnd, IDM_AVI);
- }
- void DisableAllMenu(){
- DisableMenu();
- MenuDisable(hMainWnd, IDM_OPENFILE);
- MenuDisable(hMainWnd, IDM_EXIT);
- }
- void EnableAllMenu(){
- EnableMenu();
- MenuEnable(hMainWnd, IDM_OPENFILE);
- MenuEnable(hMainWnd, IDM_EXIT);
- }
- void LoadFirstTime(HINSTANCE hInstance, HWND hWnd){
-
- o.version=VERSION;
- o.options.showDetailsInProgress=true;
- o.options.idctType= IDCT_MMX;
- o.options.selectedTab=0;
-
- o.options.warn_overwrite= true;
- o.options.shutdown = false;
- o.options.lurk_size = 5000;
-
- o.options.priority = NORMAL_PRIO;
- o.options.crop = false;
- o.options.cropTopOffset = 0;
- o.options.cropHeight = 288;
- o.options.cropLeftOffset= 0;
- o.options.cropWidth = 352;
-
-
- o.options.letterbox = false;
- o.options.letterboxTop = 0;
- o.options.letterboxBottom= 0;
- o.options.letterboxLeft = 0;
- o.options.letterboxRight= 0;
-
- o.options.deinterlace = 0;
- o.options.blend = 1;
- o.options.threshold = 20;
-
- o.options.keepAspect = true;
- o.options.recons_progressive = true;
-
- o.options.InterpolatedWidth=352;
- o.options.InterpolatedHeight=288;
- o.options.compileWhole = true;
- o.options.framesToCompile = 3000;
- o.options.displayVideo = true;
- o.options.filter = FILTER_TABLEBICUBIC;
- o.options.hOutputFile = NULL;
- o.options.timeBase.scale = 25;
- o.options.timeBase.sampleSize = 1;
- o.options.timeBase.value = 0xFFFFFF;
- o.options.presentFrame = 0;
- o.options.outputFormat = 0;
- o.options.syncVideo = true;
- o.options.audioMode = DO_AUDIO;
- o.options.outSampleFrequency = 44100;
- o.options.sampleFreqSameAsInput = true;
-
- o.options.firstTimeRunning= true;
-
-
- strcpy(o.options.audioOutFile, "C:\\audioOut");
- strcpy(o.options.outputFile, "C:\\videoOut");
- SaveRegistryConfig(&o);
- LoadRegistryConfig(&o);
- DialogBox(hInstance, MAKEINTRESOURCE(IDD_WELCOME), hWnd, (DLGPROC)WelcomeDlg);
- }
-
-
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
-
- hInst = hInstance; // Store instance handle in our global variable
- hMainWnd=hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX ,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
-
- if (!hWnd)
- {
- return FALSE;
- }
- //Load plugins
- if(!LoadPlugins(&rs.plugs)){
- PrintError(ERROR_PLUGIN, (int)hMainWnd, 0);
- exit(0);
- }
-
- //Starting options
- if(LoadRegistryConfig(&o)==0) //This is the first time. Load defaults
- {
- LoadFirstTime(hInstance, hWnd);
- }
- else
- {
- //Key existed. Check version
- if(o.version!=VERSION)
- LoadFirstTime(hInstance, hWnd);
- else
- o.options.firstTimeRunning=false;
- }
- if(rs.plugs.OutPluginCount <= o.options.outputFormat)
- o.options.outputFormat = 0;
- rs.selected_out_plug = o.options.outputFormat;
- SetTextMenu();
- //Miscelaneous Inits
- SetWindowSize();
- compileAborted=true;
- playerStopped=true;
- rs.startFilePos=0;
- rs.SD=NULL;
- rs.audio = NULL;
- rs.video = NULL;
- rs.video_inp = NULL;
- rs.audio_inp = NULL;
- rs.lng->SetLanguage( o.options.selected_language );
- SetTextMenu();
- rs.working_mode = FILE_MODE;
-
- hEncodingThread = CreateThread(NULL, // no security attributes
- 0, // use default stack size
- (LPTHREAD_START_ROUTINE) EncodingThread,
- (LPVOID)NULL, // param to thread func
- 0, // creation flag
- &ENCIDThread); // thread identifier
- if(hEncodingThread)
- EncoderMessage( LOAD_LIBRARY );
-
-
- ShowWindow(hWnd, nCmdShow);
-
- UpdateWindow(hWnd);
- MMX_check();
- if(MMX_capable)
- MMX_enable(1);
-
- //hdd=DrawDibOpen();
- return TRUE;
- }
-
-
-
-
-
- Crop(ui8 *image, int xsize,int ysize,int topCrop,int bottomCrop){
-
- int topStart;
- ui8 *imagetemp;
-
- if(topCrop>ysize)
- topCrop=ysize;
- if(bottomCrop>ysize)
- bottomCrop=ysize;
- // First byte, image bottom
- ZeroMemory(image, bottomCrop*xsize*4);
- // bottom
- topStart=ysize-topCrop;
-
- imagetemp = image + topStart*xsize*4;
- ZeroMemory(imagetemp, topCrop*xsize*4);
-
-
- return 1;
- }
-
- //
- // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // WM_COMMAND - process the application menu
- // WM_PAINT - Paint the main window
- // WM_DESTROY - post a quit message and return
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
- TCHAR szHello[MAX_LOADSTRING];
-
- RECT rt;
- HDC hdcCompatible;
- HBITMAP hbm;
- HWND hDlg;
- HGDIOBJ hprevious;
- TVideoOptions video_opt;
- LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
- CompileEntryFunc CompileEntry;
-
-
- switch (message)
- {
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // Handling custom menus
- // Language
- if(wmId >= 10000 && wmId <10256){
- rs.lng->SetLanguage(wmId-10000);
- SetTextMenu();
- }
- // Plugins
- if(wmId >= 10300 && wmId <(10300+MAX_OUT_PLUGINS) ){
- rs.selected_out_plug = wmId - 10300;
- SetTextMenu();
- }
-
- // Parse the menu selections:
- switch (wmId)
- {
-
- case IDM_OPENFILE:
- OpenFile();
- break;
- case IDM_OPENDVD:
- if(!OpenDVDFile())
- PrintError(IFO_PARSING_ERROR,(int)hMainWnd, 0);
- break;
- case IDM_MPEG1OPTIONS:
- CompileEntry = rs.plugs.outPlugs[rs.selected_out_plug].CompileEntry;
- //User Preferences
-
- if(CompileEntry){
- FilePrefsRec.compilerPrefs = rs.plugs.outPlugs[rs.selected_out_plug].settings;
- (CompileEntry)(compGetFilePrefs, &stdParms, (LONG)&FilePrefsRec,0);
- if( FilePrefsRec.compilerPrefs ){
- if(!rs.plugs.outPlugs[rs.selected_out_plug].settings)
- rs.plugs.outPlugs[rs.selected_out_plug].settings = (char *)malloc(OUT_PLUG_MEM);
-
- //ZeroMemory( rs.plugs.outPlugs[rs.selected_out_plug].settings , OUT_PLUG_MEM);
- memcpy( rs.plugs.outPlugs[rs.selected_out_plug].settings , FilePrefsRec.compilerPrefs, _msize(FilePrefsRec.compilerPrefs) );
-
- }
-
- }
- //In FilePrefsRec lay Plugin options
- break;
- case IDM_START2:
-
- // Check for output files
- char myFile[1024];
- // Init Demuxer
- myClock=0;
-
- DecodedImage.init(&MPEGImage, rs.video->pictureWidth, rs.video->picutreHeight, 32);
- DecodedImage.AlignTo4();
-
- if(o.options.warn_overwrite){
- FILE *input = NULL;
- strcpy( myFile, o.options.audioOutFile);
- if(input=fopen(add_file_ext(myFile), "rb")){
- fclose(input);
- if(MessageBox(hWnd, GS(WARN_OVERWRITE), GS(WARNING), MB_YESNO) == IDNO)
- break;
- }
- strcpy( myFile, o.options.outputFile);
- if(input=fopen(add_file_ext(myFile), "rb")){
- fclose(input);
- if(MessageBox(hWnd, GS(WARN_OVERWRITE), GS(WARNING), MB_YESNO) == IDNO)
- break;
- }
-
- }
- #ifdef TESTING_AUDIO_OUTPUT
- test_sound_file = fopen("c:\\salida","wb");
- #endif
- #ifdef TESTING_MULTIAUDIO
- if(o.options.outputFormat==AVI_PLUG)
- (CompileEntry)(99, &stdParms, 2, 0);
- #endif
- // Get post processing options from config
- GetPPostConfig(&o, &rs.pp);
- // Set additional parameters
- rs.pp.iDAR = rs.video->DAR;
-
- InitDibDisplay(PPOST_WIDTH(rs.pp) , PPOST_HEIGHT(rs.pp));
- // Start Post processing
- PostProcessingStart(&DecodedImage, &Display, &rs.pp);
- rs.pp.myClock = myClock;
-
-
-
- frameSpan= (i64)((1/((double)(o.options.timeBase.scale)/
- (double)(o.options.timeBase.sampleSize)))*(double)MPEG2_CLK_REF);
- imageWaiting=false;
- firstTime=true;
- audioPrevFrame=0;
- //Seek mobs at the begginnig of the compile
- rs.video->SeekVideo(rs.startFilePos);
- if(rs.audio)
- rs.audio->SeekAudio(rs.startFilePos);
- #ifdef TESTING_MULTIAUDIO
- rs.audio2->SeekAudio(rs.startFilePos);
- #endif
- //Start video
- video_opt.idctType = o.options.idctType;
- video_opt.recons_progressive = o.options.recons_progressive;
- if(rs.video) rs.video->Start(&video_opt);
- else return 0;
- //Decode first frame to get PTS info for audio
- //video->GetFrame(&pInfo);
- firstImageAlreadyDecoded=false;
- if(o.options.sampleFreqSameAsInput)
- o.options.outSampleFrequency=rs.audio->sampleRate;
- //Start audio with that PTS info.
- if(rs.audio) {
- switch( o.options.audioMode){
- case DSC:
- rs.audio->Start(o.options.audioOutFile, o.options.audioMode);
- break;
- case DO_AUDIO:
- rs.audio->Start(o.options.outSampleFrequency,pInfo.imagePTS, o.options.audioMode);
- break;
- }
- }
- #ifdef TESTING_MULTIAUDIO
- rs.audio2->Start(o.options.outSampleFrequency,pInfo.imagePTS, o.options.audioMode);
- #endif
-
- EncoderMessage( ENCODE );
- DisableAllMenu();
- hDlg=CreateDialog(hInst,MAKEINTRESOURCE(IDD_PROGRESS), hWnd, (DLGPROC) ProgressDlg);
- ShowWindow(hDlg, SW_SHOW);
- /*DialogBox( hInst, // handle to application instance
- MAKEINTRESOURCE(IDD_PROGRESS), // identifies dialog box template
- hWnd, // handle to owner window
- (DLGPROC) ProgressDlg // pointer to dialog box procedure
- );
- */
- break;
- /* case IDM_MPEG:
- o.options.outputFormat=MPEG_PAN;
- SetMenuMPEG();
- CompileEntry=PanEntry;
- break;
- case IDM_AVI:
- SetMenuAVI();
- CompileEntry=AVIplugEntry;
- o.options.outputFormat=AVI_PLUG;
- break;
- */ case IDM_PLAY:
- DialogBox( hInst, MAKEINTRESOURCE(IDD_PLAYER), hMainWnd, (DLGPROC)PlayerDlg);
- break;
- case IDM_OUTPUTCONFIG:
- DialogBox(hInst, (LPCTSTR)IDD_OUTPUT_OPTIONS, hWnd, (DLGPROC)OutputOptionsDlg);
- break;
- case IDM_ABOUT:
- /* TAudioPlayback ap;
- unsigned char *data;
-
- ap.n_channels = 2;
- ap.sample_freq = rs.audio->sampleRate;
- ap.chunk_size = 4096*6;
-
-
-
- AudioPlaybackStart(&ap);
- rs.audio->Start(rs.audio->sampleRate, 0, DO_AUDIO);
- while(1)
- {
- rs.audio->GetSamples(0, (short **)&data, (4096*6)>>2);
- OutputDebugString("starting write");
- AudioPlaybackWrite(&ap, data);
- OutputDebugString("ending write");
- }
- AudioPlaybackStop(&ap);
- */
- DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
-
- break;
- case IDM_EXIT:
-
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_PAINT:
-
-
- hdc = BeginPaint(hWnd, &ps);
- if( compileAborted && playerStopped ){
- // TODO: Add any drawing code here...
- hdcCompatible = CreateCompatibleDC(hdc);
- hbm= LoadBitmap( hInst, MAKEINTRESOURCE(IDB_FONDO));
- hprevious = SelectObject(hdcCompatible, hbm);
- GetClientRect(hWnd, &rt);
- BitBlt(hdc,
- 0,0,
- rt.right-rt.left, rt.bottom-rt.top,
- hdcCompatible,
- 0,0,
- SRCCOPY);
- SelectObject( hdcCompatible, hprevious);
- DeleteObject( hbm );
- DeleteDC( hdcCompatible );
- }
-
- //DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- o.options.selected_language = rs.lng->GetLanguage();
- o.options.outputFormat = rs.selected_out_plug;
-
- SaveRegistryConfig(&o);
- FlushPluginSettings( &rs.plugs );
- ClosePlugins(&rs.plugs);
- EncoderMessage( UNLOAD_LIBRARY );
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
-
- // Mesage handler for about box.
- LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- SetText(hDlg, GS(ABOUT_TITLE) );
- DlgSetText( hDlg, R_ABOUT_GPL, GS(R_ABOUT_GPL) );
- DlgSetText( hDlg, R_ABOUT_TRANSLATOR, GS(R_ABOUT_TRANSLATOR) );
- DlgSetText( hDlg, R_ABOUT_DROPLINE, GS(R_ABOUT_DROPLINE) );
- DlgSetText( hDlg, R_ABOUT_ATHLON, GS(R_ABOUT_ATHLON) );
-
- return TRUE;
-
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
-
- // Mesage handler for console
- LRESULT CALLBACK DlgConsole(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- return TRUE;
-
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
-
- void SetPriority(int prio){
- switch (prio){
- case IDLE_PRIO:
- SetThreadPriority(hEncodingThread, THREAD_PRIORITY_IDLE);
- break;
- case NORMAL_PRIO:
- SetThreadPriority(hEncodingThread, THREAD_PRIORITY_NORMAL);
- break;
- case HIGH_PRIO:
- SetThreadPriority(hEncodingThread, THREAD_PRIORITY_HIGHEST);
- break;
- case HIGHEST_PRIO:
- SetThreadPriority(hEncodingThread, THREAD_PRIORITY_TIME_CRITICAL);
- break;
- }
- }
- // Mesage handler for about box.
- LRESULT CALLBACK ProgressDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- double porciento;
- char szTemp[MAXFILENAME];
- static char temp_filename[MAXFILENAME];
- static int i,j,k,w,h;
- static DWORD remaining_time,present_time,elapsed_time,start_time;
- static __int64 startPos=0;
- RECT rc;
-
- switch (message)
- {
- case WM_SYSCOMMAND:
- /* if(wParam==SC_MINIMIZE)
- SendMessage(hMainWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
- if(wParam==SC_RESTORE)
- SendMessage(hMainWnd, WM_SYSCOMMAND, SC_RESTORE, 0);*/
- break;
- case WM_TIMER:
- if(!compileIsPaused){
-
- //Process information
- //input
- DlgSetText(hDlg, IDC_IVIDEORES, rs.video->sVideoSize);
- DlgSetText(hDlg, IDC_IFRAMERATE, rs.video->sFrameRate);
- DlgSetText(hDlg, IDC_IASPECT, rs.video->sAspectRatio);
- DlgSetText(hDlg, IDC_IVIDEOSTRUCT ,rs.video->sProgressive);
- DlgSetText(hDlg, IDC_IFILE, rs.video->GetVideoFileName());
- if(rs.audio){
- (rs.audio->isAC3) ? (DlgSetText(hDlg, IDC_IAUDIO, "AC3 Stream")): (DlgSetText(hDlg, IDC_IAUDIO, "MPEG audio stream"));
- (rs.audio->isAC3) ? (DlgSetText(hDlg, IDC_IAUDIOFREQ, rs.audio->sSampleRate)): (DlgSetText(hDlg, IDC_IAUDIOFREQ, ""));
- (rs.audio->isAC3) ? (DlgSetText(hDlg, IDC_IAUDIOBITRATE, rs.audio->sBitRate)): (DlgSetText(hDlg, IDC_IAUDIOBITRATE, ""));
- (rs.audio->isAC3) ? (DlgSetText(hDlg, IDC_IAUDIOMODE, rs.audio->sAudioMode)): (DlgSetText(hDlg, IDC_IAUDIOMODE, ""));
- }
- // Graph
- if(rs.plugs.outPlugs[rs.selected_out_plug].fileType == VXXtype)
- DrawGraph(hDlg, &rs.gr);
- //outputs
- sprintf(szTemp, "%d x %d pixels", PPOST_WIDTH(rs.pp), PPOST_HEIGHT(rs.pp));
- DlgSetText( hDlg, IDC_OVIDEORES, szTemp);
- StringFrameRate(o.options.timeBase.scale, szTemp);
- DlgSetText( hDlg, IDC_OFRAMERATE, szTemp);
- if(o.options.keepAspect) DlgSetText( hDlg, IDC_OKEEPASPECT, "Yes"); else DlgSetText( hDlg, IDC_OKEEPASPECT, "No");
- DlgSetText( hDlg, IDC_OVIDEOFILE, temp_filename);
- if(o.options.audioMode == DSC)
- DlgSetText( hDlg, IDC_OAUDIOFILE, o.options.audioOutFile);
- if(o.options.audioMode==DSC)
- DlgSetText( hDlg, IDC_OAUDIO, "Direct stream copy");
- else if(o.options.audioMode==DO_AUDIO)
- DlgSetText( hDlg, IDC_OAUDIO, "Normal audio decoding");
- else if(o.options.audioMode==NO_AUDIO)
- DlgSetText( hDlg, IDC_OAUDIO, "Audio processing disabled");
- if(o.options.audioMode==DO_AUDIO){
- sprintf(szTemp, "%d Hz", o.options.outSampleFrequency);
- DlgSetText( hDlg, IDC_OAUDIOFREQ,szTemp);
- }
- else{
- DlgSetText( hDlg, IDC_OAUDIOFREQ,"");
- }
- if(o.options.crop)
- DlgSetText(hDlg, IDC_OCROP, "Yes");
- else
- DlgSetText(hDlg, IDC_OCROP, "No");
-
-
- if(!o.options.compileWhole) // Frames specified
- {
- porciento= ((double)o.options.presentFrame/(double)o.options.framesToCompile)*100;
- SendDlgItemMessage( hDlg, IDC_PROGRESS1, PBM_SETPOS, (ui32)porciento , 0);
- if( o.options.presentFrame <2 && i<200){
- i++;
- strcpy( szTemp, "Starting Up ..");
- for(j=1; j<=i; j++){
- strcat(szTemp, "..");
- }
- SetDlgItemText( hDlg, IDC_FRAMENO, szTemp);
- }
- else{
- sprintf(szTemp, "Processing frame %d of %d ", o.options.presentFrame, o.options.framesToCompile);
- SetDlgItemText( hDlg, IDC_FRAMENO, szTemp);
- }
- }
- else //Compile whole file
- {
- porciento= ((double)(rs.video->GetVideoPos() -startPos)/(double)(rs.video->GetVideoSize()-startPos))*100;
- SendDlgItemMessage( hDlg, IDC_PROGRESS1, PBM_SETPOS, (ui32)porciento , 0);
- if( o.options.presentFrame <2 && i<1000){
- i++;
- strcpy( szTemp, "Starting Up .");
- for(j=1; j<=i; j++){
- strcat(szTemp, ".");
- }
- SetDlgItemText( hDlg, IDC_FRAMENO, szTemp);
- }
- else{
- sprintf(szTemp, "Processing frame %d", o.options.presentFrame);
- SetDlgItemText( hDlg, IDC_FRAMENO, szTemp);
- }
-
- }
-
- sprintf(szTemp, "%d %%", (int)porciento);
- SetDlgItemText( hDlg, IDC_PERCENT, szTemp);
- //Pilla tiempo actual
- if(o.options.presentFrame == 0)
- start_time=GetTickCount();
- present_time=GetTickCount();
- //Duracion simulacion
- MillisecondsToTime( szTemp, elapsed_time=present_time-start_time);
- SetDlgItemText( hDlg, IDC_ELAPSED, szTemp);
- // FPS
- if(elapsed_time){
- sprintf(szTemp, "%.2f", (double)o.options.presentFrame/ ((double)elapsed_time/1000) );
- DlgSetText( hDlg, IDC_FPS, szTemp);
- }
- //Tiempo restante
- remaining_time= (DWORD)((100-porciento)/porciento)*elapsed_time;
- MillisecondsToTime( szTemp, remaining_time);
- SetDlgItemText( hDlg, IDC_REMAINING, szTemp);
- sprintf( szTemp, "FlasK Encoding %d%% %c ", (int)porciento, chars[k++%8]);
- SetWindowText((HWND)hMainWnd, szTemp);
- //mpeg TIME
- /*sprintf( szTemp, "MPEG time: %02d:%02d:%02d:%02d", video->timeCode.hour,
- video->timeCode.minute,
- video->timeCode.second,
- video->timeCode.frame);
- SetDlgItemText( hDlg, IDC_MPEGTIME, szTemp); */
-
-
-
- if( compileAborted /*&& audioFinished*/ )
- {
- //Stop audio and video
- if(rs.video)
- rs.video->Stop();
- if(rs.audio)
- rs.audio->Stop();
- #ifdef TESTING_MULTIAUDIO
- rs.audio2->Stop();
- #endif
- #ifdef TESTING_AUDIO_OUTPUT
- fclose(test_sound_file);
- #endif
-
- PostProcessingStop(&rs.pp);
- SetWindowText((HWND)hMainWnd, "FlasK MPEG");
- KillTimer( hDlg, 1);
- EnableAllMenu();
- EndDialog(hDlg, LOWORD(wParam));
-
- //And turndown computer if suitable
- if(o.options.shutdown && (o.options.presentFrame >= 1000))
- ExitWindowsEx( EWX_SHUTDOWN, 0);
- return TRUE;
- }
- }
- break;
- case WM_INITDIALOG:
- // Graph init
- GraphStart(&rs.gr);
-
- //Set combo box to normal and set
- SendDlgItemMessage(hDlg, IDC_PRIO, CB_ADDSTRING, 0, (LPARAM) "Idle");
- SendDlgItemMessage(hDlg, IDC_PRIO, CB_ADDSTRING, 0, (LPARAM) "Normal");
- SendDlgItemMessage(hDlg, IDC_PRIO, CB_ADDSTRING, 0, (LPARAM) "High");
- SendDlgItemMessage(hDlg, IDC_PRIO, CB_ADDSTRING, 0, (LPARAM) "Highest");
- SendDlgItemMessage(hDlg, IDC_PRIO, CB_SETCURSEL, 1, 0);
- SetPriority( NORMAL_PRIO );
-
- if(o.options.displayVideo)
- SendDlgItemMessage( hDlg, IDC_DISPLAYOUTPUT, BM_SETCHECK, BST_CHECKED ,0);
- else
- SendDlgItemMessage( hDlg, IDC_DISPLAYOUTPUT, BM_SETCHECK, BST_UNCHECKED ,0);
-
- i=0;
- compileIsPaused=false;
-
- start_time=GetTickCount();
- startPos=rs.video->GetVideoPos();
- strcpy(temp_filename, o.options.outputFile);
- add_file_ext(temp_filename);
- progressTimer= SetTimer(hDlg, // handle of window for timer messages
- 1, // timer identifier
- 500, // time-out value
- NULL // address of timer procedure
- );
-
- GetWindowRect(hDlg, &rc);
- w = rc.right - rc.left;
- h = rc.bottom - rc.top;
- if(rs.plugs.outPlugs[rs.selected_out_plug].fileType != VXXtype)
- WindowResize( hDlg, w, (int)(h*0.76));
- return TRUE;
-
- case WM_COMMAND:
-
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- abortCompile = true;
- }
- switch(LOWORD(wParam))
- {
- case IDC_PRIO:
- if(HIWORD(wParam)==CBN_SELCHANGE)
- SetPriority( SendDlgItemMessage(hDlg, IDC_PRIO, CB_GETCURSEL, 0,0) );
- break;
- case IDC_PAUSE:
- if(compileIsPaused){
- DlgSetText(hDlg, IDC_PAUSE, "&Pause");
- pauseCompile=false;
- ResumeThread(hEncodingThread);
- }
- else{
- DlgSetText(hDlg, IDC_PAUSE, "&Resume");
- pauseCompile=true;
- }
- break;
- case IDC_DISPLAYOUTPUT:
- if(o.options.displayVideo)
- o.options.displayVideo=false;
- else
- o.options.displayVideo=true;
- break;
- case IDC_DETAILS:
-
- return TRUE;
- }
-
- break;
-
- }
- return FALSE;
- }
-
- ///////////////////////////////////
- // fgetAudio : PREMIERE AUDIO STUB
- ////////////////////////////////////
- int fgetAudio ( long frame, long *frameCount, long *size, long offset, BufferReturnType theBuffer, long compileSeqID )
- {
- static char audiodata[10240];
- static char *newblock;
-
- #ifdef _DEBUG
- sprintf(szTemp, "GetAudio. Frame:%d FrameCount:%d Offset:%d ID:%d\n", frame, *frameCount, offset, compileSeqID );
- OutputDebugString( szTemp);
- #endif
-
- *size = CompileInfo.outputRec.audchunksize;
- #ifdef TESTING_MULTIAUDIO
- if(frame <1000000)
- audio->GetSamples( frame, (short **)&newblock , CompileInfo.outputRec.audchunksize>>2 );
- else
- rs.audio2->GetSamples( frame, (short **)&newblock , CompileInfo.outputRec.audchunksize>>2 );
- #else
-
- if(!(frame <= audioPrevFrame))
- rs.audio->GetSamples( frame, (short **)&newblock , CompileInfo.outputRec.audchunksize>>2 );
- #endif
-
- #ifdef TESTING_AUDIO_OUTPUT
- if(frame <1000000)
- fwrite(newblock, *size, 1, test_sound_file);
- #endif
-
- audioPrevFrame = frame;
- *theBuffer = &newblock;
-
- return comp_ErrNone;
- }
-
- long fgetBlipMax ( long compileSeqID )
- {
- printf("GetBlipMax. ID:%d\n",compileSeqID);
- return CompileInfo.outputRec.audchunksize;
- }
- ///////////////////////////////////
- // fgetFrame : PREMIERE VIDEO STUB
- ////////////////////////////////////
- int fgetFrame ( long frame, void **buffer, long *rowbytes, compGetFrameReturnRec *getFrameReturn, char getCompressed, long compileSeqID )
- {
- static compGetFrameReturnRec ReturnRecord;
- char szTemp[260];
- static HANDLE hDC;
-
- YUVImageTag *yuv_frame=NULL;
-
- if(rs.plugs.outPlugs[rs.selected_out_plug].fileType == VXXtype)
- UpdateGraph(&rs.gr, (TAVIPlugGraphInfo *)compileSeqID);
-
- if(firstTime){
- myClock = 0;
- firstTime = false;
- }
- if(!firstImageAlreadyDecoded){
- while(1){
- if(!imageWaiting){
- if(!rs.video->GetFrame(&pInfo, &yuv_frame)){
- // If bbMPEG is being used
- // don't abort
- if(rs.plugs.outPlugs[rs.selected_out_plug].fileType!='\0MPG')
- abortCompile=true;
- break;
- }
- /* if(!pInfo.SCRlocked || firstTime){//Update clocks
- myClock=pInfo.imagePTS- (frameSpan/2);
- firstTime=false;
- }*/
- }
- //Check if th image has to be displayed in this frame
- if((pInfo.imagePTS >= myClock) && (pInfo.imagePTS<(myClock+frameSpan))){
- imageWaiting=false;
- break;
- }
- else if( pInfo.imagePTS >= (myClock+frameSpan) ){
- imageWaiting=true;
- break;
- }
- //Image has a PTS lower than myClock. Drop it
- imageWaiting=false;
- }
- }
- else{//FirstImageAlreadyDecoded
- firstImageAlreadyDecoded=false;
- }
- myClock+=frameSpan;
- //If GetFrame returns 0, video bitstream is over
-
- if(rs.audio)
- if(o.options.audioMode==DSC)//If we are in Direct Stream Copy audio mode
- rs.audio->GetAudioDSC(pInfo.imagePTS, rs.video->GetVideoPos());
-
- YUVtoRGB(yuv_frame, MPEGImage);
-
- rs.pp.myClock = myClock;
- PostProcess(&DecodedImage, &Display, &rs.pp);
-
- if(o.options.displayVideo)
- {
- hDC = GetDC(hMainWnd);
- SetDIBitsToDevice((HDC) hDC,0,0,PPOST_WIDTH(rs.pp),PPOST_HEIGHT(rs.pp),
- 0,0,0,PPOST_HEIGHT(rs.pp), (ui8 *)Display.Address(),&DibInfo,DIB_RGB_COLORS);
- ReleaseDC( hMainWnd, (HDC)hDC);
- }
-
- if(pauseCompile){
- compileIsPaused=true;
- SuspendThread(hEncodingThread);
- compileIsPaused=false;
- }
-
- *buffer = Display.Address();
- *rowbytes = 4*PPOST_WIDTH(rs.pp);
-
- ReturnRecord.returnVal = comp_ErrNone;
- ReturnRecord.repeatCount = 0;
- ReturnRecord.makeKeyFrame = FALSE;
- ReturnRecord.frameIsCompressed = FALSE;
- ReturnRecord.startframe = frame;
- ReturnRecord.frameOnMarker = FALSE;
-
-
- o.options.presentFrame++;
- sprintf(szTemp, "GetFrame. Frame:%d ID:%d\n", frame, compileSeqID );
-
- if(abortCompile || o.options.presentFrame > o.options.framesToCompile )
- ReturnRecord.returnVal = comp_CompileAbort;
-
- *getFrameReturn = ReturnRecord;
- return ReturnRecord.returnVal;
- }
-
-
- DWORD WINAPI EncodingThread(LPVOID *nada)
- {
- static HINSTANCE hinstLib,AVIplugLib,PANplugLib;
- BOOL fRunTimeLinkSuccess = FALSE;
- int round;
-
- CompileEntryFunc CompileEntry;
-
- hinstLib = NULL;
- PANplugLib = NULL;
-
-
- while(1)
- {
- switch(sendEncodingThread){
- case UNLOAD_LIBRARY:
- break;
- case LOAD_LIBRARY:
-
- break;
- case ENCODE:
-
- o.options.presentFrame=0;
- //Setup compiler plugin info
- CompileInfo.compilerID=rs.plugs.outPlugs[rs.selected_out_plug].CompilerID;
- CompileInfo.compilerPrefs=rs.plugs.outPlugs[rs.selected_out_plug].settings;
- //CompOutputRec
- CompileInfo.outputRec.doVideo=1;
- CompileInfo.outputRec.doAudio= (o.options.audioMode==DO_AUDIO);
- //vidCompression
- CompileInfo.outputRec.vidCompression.subtype = rs.plugs.outPlugs[rs.selected_out_plug].fileType;
- CompileInfo.outputRec.vidCompression.depth = 16;
- CompileInfo.outputRec.vidCompression.recompressWhen=recompNever;
-
- //CompileInfo.subtype=un
- //PostProcessing int gammaCorrection;
- CompileInfo.postProcessing.gammaCorrection=1;
- CompileInfo.outputRec.width= PPOST_WIDTH(rs.pp) ;
- CompileInfo.outputRec.height= PPOST_HEIGHT(rs.pp);
- //compTimebaseRec
- CompileInfo.outputRec.timebase.scale= o.options.timeBase.scale;
- CompileInfo.outputRec.timebase.sampleSize= o.options.timeBase.sampleSize;
- CompileInfo.outputRec.timebase.value= o.options.timeBase.value;
- CompileInfo.outputRec.fieldType= compFieldsNone;
-
- //compPostProcessing
- //compFileSpec
- strcpy( CompileInfo.outputFile.name, o.options.outputFile );
- add_file_ext(CompileInfo.outputFile.name);
- //Now we create the options file
- /*options.hOutputFile = CreateFile(options.outputFile, // create file
- GENERIC_WRITE,// open for writing
- 0, // do not share
- NULL, // no security
- CREATE_ALWAYS,// overwrite existing
- FILE_ATTRIBUTE_NORMAL |// normal file
- FILE_FLAG_OVERLAPPED, // asynchronous I/O
- NULL);// no attr. template
-
- if (options.hOutputFile == INVALID_HANDLE_VALUE)
- {
- //ErrorHandler("Could not open file."); // process error
- break;
- }*/
- WAVEFORMATEX AudioCompressionRecord;
-
- AudioCompressionRecord.wFormatTag = WAVE_FORMAT_PCM;
- AudioCompressionRecord.nChannels = 2;
- AudioCompressionRecord.nSamplesPerSec = o.options.outSampleFrequency;
- AudioCompressionRecord.nAvgBytesPerSec = o.options.outSampleFrequency*4;
- AudioCompressionRecord.nBlockAlign = 4;
- AudioCompressionRecord.wBitsPerSample = 16;
- AudioCompressionRecord.cbSize = 0;
-
- memcpy(&CompileInfo.outputRec.audCompression.AudCompRec, (char *)&AudioCompressionRecord, 30);
- CompileInfo.outputRec.audCompression.subtype = 0;
-
- CompileInfo.outputRec.fieldType = compFieldsNone;
- CompileInfo.outputRec.audrate = o.options.outSampleFrequency;
- CompileInfo.outputRec.audsamplesize = 16;
- CompileInfo.outputRec.stereo = TRUE;
- CompileInfo.outputRec.audchunksize = (int)((CompileInfo.outputRec.audrate)/((double)o.options.timeBase.scale/(double)o.options.timeBase.sampleSize)*4); //samples/Frame * 2bytes/sample * 2channels
- //round chunksize to the next 4 boundary. A stereo sample takes 4 bytes
- CompileInfo.outputRec.audchunksize += (round=CompileInfo.outputRec.audchunksize % 4) ? (4-round): round;
-
-
- CompileInfo.outputRec.audInterleave = prInterleave1Frame;
-
- o.options.hOutputFile=NULL;
- //To close: CloseHandle
- CompileInfo.outputFileRef= (compFileRef) o.options.hOutputFile;
- CompileInfo.startFrame= 1;
- CompileInfo.endFrame= o.options.framesToCompile;
- CompileInfo.compileSeqID= 0;
- // Awake decoding process
- //Decoding process parameters
-
-
- decodingFinished=TRUE;
-
- abortCompile=false;
- compileAborted=false;
-
-
- //Start compile
- CompileEntry = rs.plugs.outPlugs[rs.selected_out_plug].CompileEntry;
- if(CompileEntry)
- (CompileEntry)(compDoCompile, &stdParms, (long)&CompileInfo,0);
- // If my decoding process was sleep awake it and inform it to abort
- abortCompile=true;
- SetWindowSize();
- InvalidateRect(hMainWnd, NULL, true);
-
-
- compileAborted=true;
-
-
- break;
- }
- SuspendThread(hEncodingThread);
-
-
- }
- return 1;
- }
-
-
- void EncoderMessage( int message )
- {
- sendEncodingThread= (EncEnum) message;
- ResumeThread(hEncodingThread);
-
- };
-
-
- ///////////////////////////////////////////////////////
- // PLAYER
- ///////////////////////////////////////////////////////
-
- HWND hPlayerDlg;
- HANDLE evPlayerStopped;
- HANDLE evFrameCaptured;
- CRITICAL_SECTION GlobalCriticalSection;
- bool SeekInProgress;
- i64 diferencia;
-
-
-
- DWORD WINAPI PlayerThread(LPVOID *nada){
-
- HDC hDC;
- static unsigned char newblock[MAX_IMAGE_MEM];
- static i64 prevTime;
- static presInfo pInfo;
- YUVImageTag *frame;
-
- while(1)
- {
- if(!rs.video->GetFrame(&pInfo, &frame)){
- switch(rs.video->GetError())
- {
- case END_OF_STREAM:
- //Stream ended. Push stop button
- SendMessage(hPlayerDlg, WM_COMMAND, MAKEWPARAM(IDC_STOP,0), 0);
- SetEvent(evPlayerStopped);
- return 0;
- break;
- case PLAYER_STOPPED:
- SetEvent(evPlayerStopped);
- return 0;
- break;
- }
-
- }
- diferencia=pInfo.imagePTS-prevTime;
- prevTime=pInfo.imagePTS;
-
- rs.audio->sysClock=pInfo.imagePTS;
- if(rs.audio)
- if(o.options.audioMode==DSC)//If we are in Direct Stream Copy audio mode
- rs.audio->GetAudioDSC(pInfo.imagePTS, rs.video->GetVideoPos());
-
- SetEvent(evFrameCaptured);
- YUVtoRGB(frame, MPEGImage);
-
- myClock+=frameSpan;
- rs.pp.myClock = myClock;
- PostProcess(&DecodedImage, &Display, &rs.pp);
-
-
- hDC = GetDC(hMainWnd);
- SetDIBitsToDevice((HDC) hDC,0,0,PPOST_WIDTH(rs.pp),PPOST_HEIGHT(rs.pp),
- 0,0,0,PPOST_HEIGHT(rs.pp), (ui8 *)Display.Address(),&DibInfo,DIB_RGB_COLORS);
- ReleaseDC( hMainWnd, (HDC)hDC);
- }
-
- }
- LRESULT CALLBACK PlayerDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static HANDLE hPlayer;
- DWORD PlayerIDThread;
- static playerTimer;
- char szTemp[256];
- int fPosition, lPosition;
- static bool userIsTracking;
- TVideoOptions video_opt;
- static int kkk;
- bool doNothing;
-
-
- switch (message)
- {
- case WM_TIMER:
- // time
- MillisecondsToTime(szTemp, rs.video->time);
- SetDlgItemText(hDlg, IDC_PLAYTIME, szTemp);
- // Total frames
- // File
- DlgSetText(hDlg, IDC_FILE, rs.video->GetVideoFileName());
- // Total file size
- sprintf( szTemp, "%d Mbytes", rs.video->GetVideoSize()/(1024*1024));
- SetDlgItemText(hDlg, IDC_FILESIZE, szTemp);
- // Video size
- DlgSetText(hDlg,IDC_VIDEOSIZE ,rs.video->sVideoSize);
- // Frame rate
- DlgSetText(hDlg,IDC_FRAMERATE ,rs.video->sFrameRate);
- //Detected FPS
- DlgSetText(hDlg,IDC_DETECTEDFPS ,rs.video->sDetectedFPS);
- // Aspect Ratio
- DlgSetText(hDlg,IDC_ASPECTRATIO ,rs.video->sAspectRatio);
- // Bitrate
- DlgSetText(hDlg,IDC_BITRATE ,rs.video->sBitrate);
- // Progressive sequence
- DlgSetText(hDlg,IDC_PROGRESSIVE ,rs.video->sProgressive);
- //DlgSetText(hDlg,IDC_PROGRESSIVE ,video->internalPTS/27000.0);
- //DlgSetText(hDlg,IDC_BITRATE ,diferencia);
-
- fPosition=true;
- lPosition= (int)(((double)(rs.video->GetVideoPos())/(double)(rs.video->GetVideoSize()))*1000);
- if( !userIsTracking && !SeekInProgress )
- SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETPOS, (WPARAM) (BOOL) fPosition,(LPARAM) (LONG) lPosition);
-
- break;
- case WM_INITDIALOG:
- myClock=0;
- frameSpan= (i64)((1/((double)(o.options.timeBase.scale)/
- (double)(o.options.timeBase.sampleSize)))*(double)MPEG2_CLK_REF);
- // Seek video
- rs.video->SeekVideo(rs.startFilePos);
- if(rs.audio)
- rs.audio->SeekAudio(rs.startFilePos);
-
- // Initialize the critical section.
- InitializeCriticalSection(&GlobalCriticalSection);
-
- // Get post processing options from config
- GetPPostConfig(&o, &rs.pp);
- // Set additional parameters
- rs.pp.iDAR = rs.video->DAR;
- InitDibDisplay(PPOST_WIDTH(rs.pp) , PPOST_HEIGHT(rs.pp));
-
- DecodedImage.init(&MPEGImage, rs.video->pictureWidth, rs.video->picutreHeight, 32);
- DecodedImage.AlignTo4();
- // Start Post processing
- PostProcessingStart(&DecodedImage, &Display, &rs.pp);
-
-
-
- hPlayerDlg=hDlg;
- SeekInProgress=false;
- userIsTracking=false;
- EnableWindow( GetDlgItem(hDlg, IDC_PLAY), true);
- EnableWindow( GetDlgItem(hDlg, IDOK), true);
- EnableWindow( GetDlgItem(hDlg, IDC_SEEKFIRST), true);
- EnableWindow( GetDlgItem(hDlg, IDC_SLIDER), false);
- EnableWindow( GetDlgItem(hDlg, IDC_STOP), false);
- SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETRANGE, (WPARAM) TRUE,(LPARAM) MAKELONG(0, 1000));
-
- evPlayerStopped=CreateEvent(NULL, TRUE, FALSE,NULL);
- evFrameCaptured=CreateEvent(NULL, TRUE, FALSE,NULL);
-
-
- playerTimer= SetTimer(hDlg, // handle of window for timer messages
- 2, // timer identifier
- 500, // time-out value
- NULL // address of timer procedure
- );
- return TRUE;
- case WM_HSCROLL:
- int nPos;
- switch(LOWORD(wParam)){
- case TB_THUMBTRACK:
- userIsTracking = true;
- break;
-
- case TB_THUMBPOSITION :
-
- //Disable trackbar
- EnterCriticalSection(&GlobalCriticalSection);
- if(SeekInProgress)
- doNothing=true;
- else{
-
- doNothing=false;
- }
- LeaveCriticalSection(&GlobalCriticalSection);
- if(doNothing) return 0;
-
- SeekInProgress=true;
- DlgDisable(hDlg, IDC_SLIDER);
- nPos = SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_GETPOS, 0, 0);
- //Stopvideo
- ResetEvent(evPlayerStopped);
- rs.video->stopDecoding=true;
- WaitForSingleObject(evPlayerStopped, 5000);
- CloseHandle(hPlayer);
- rs.video->Stop();
- if(rs.audio)
- if(o.options.audioMode==DSC)
- rs.audio->Stop();
- //Seek
- rs.startFilePos=(i64)(((double)nPos/1000.0)*(double)(rs.video->GetVideoSize()));
- rs.video->SeekVideo(rs.startFilePos);
- if(rs.audio)
- rs.audio->SeekAudio(rs.startFilePos);
- //
- video_opt.idctType = o.options.idctType;
- video_opt.recons_progressive = o.options.recons_progressive;
- myClock=0;
- rs.video->Start(&video_opt);
- if(rs.audio)
- if(o.options.audioMode==DSC)
- rs.audio->Start(o.options.audioOutFile, o.options.audioMode);
-
- hPlayer=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) PlayerThread,(LPVOID)NULL,0,&PlayerIDThread);
- ResetEvent(evFrameCaptured);
- WaitForSingleObject(evFrameCaptured, 5000);
- userIsTracking=false;
- SeekInProgress=false; //if we were seeking
- EnableWindow( GetDlgItem(hDlg, IDC_SLIDER), true);
- break;
- }
- break;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- CloseHandle(evPlayerStopped);
- CloseHandle(evFrameCaptured);
-
-
- PostProcessingStop(&rs.pp);
- KillTimer( hDlg, 2);
- SetWindowSize();
- InvalidateRect(hMainWnd, NULL, true);
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- switch( LOWORD(wParam)){
- case IDC_SEEKFIRST:
- rs.startFilePos=0;
- rs.video->SeekVideo(rs.startFilePos);
- if(rs.audio)
- rs.audio->SeekAudio(rs.startFilePos);
- break;
- case IDC_PLAY:
- playerStopped=false;
- //Grey PLAY AND EXIT
- EnableWindow( GetDlgItem(hDlg, IDC_PLAY), false);
- EnableWindow( GetDlgItem(hDlg, IDOK), false);
- EnableWindow( GetDlgItem(hDlg, IDC_SEEKFIRST), false);
- EnableWindow( GetDlgItem(hDlg, IDC_SLIDER), true);
- EnableWindow( GetDlgItem(hDlg, IDC_STOP), true);
-
- video_opt.idctType = o.options.idctType;
- video_opt.recons_progressive = o.options.recons_progressive;
- myClock=0;
- rs.video->Start(&video_opt);
- if(rs.audio)
- if(o.options.audioMode==DSC)
- rs.audio->Start(o.options.audioOutFile, o.options.audioMode);
-
-
- hPlayer=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) PlayerThread,(LPVOID)NULL,0,&PlayerIDThread);
- break;
- case IDC_STOP:
-
- EnableWindow( GetDlgItem(hDlg, IDC_SLIDER), false);
- EnableWindow( GetDlgItem(hDlg, IDC_STOP), false);
-
- ResetEvent(evPlayerStopped);
- rs.video->stopDecoding=true;
- WaitForSingleObject(evPlayerStopped, 5000);
-
- CloseHandle(hPlayer);
- rs.video->Stop();
- if(rs.audio)
- if(o.options.audioMode==DSC)
- rs.audio->Stop();
- EnableWindow( GetDlgItem(hDlg, IDC_PLAY), true);
- EnableWindow( GetDlgItem(hDlg, IDOK), true);
- EnableWindow( GetDlgItem(hDlg, IDC_SEEKFIRST), true);
- playerStopped=true;
- break;
-
- }
- break;
- }
- return FALSE;
- }
-