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

  1. /* 
  2.  *  FileOpen.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. #include "FlasKMPEG.h"
  26. #include "RunState.h"
  27. #include "error.h"
  28. #include ".\Video\misc_tables.h"
  29. #include ".\FileOpen.h"
  30. #include ".\mism.h"
  31. #include <string>
  32.  
  33. using namespace std;
  34.  
  35. extern TConfig   o;
  36. extern TRunState rs;
  37. extern HWND hMainWnd;
  38. extern void EnableMenu();
  39. extern void DisableMenu();
  40.  
  41. extern HINSTANCE hInst;
  42. extern char          program_directory[MAX_PATH];
  43.  
  44. const char *GetFilterString()
  45. {
  46.   static string pFilter;
  47.   string pExtensions;
  48.   
  49.   pFilter.empty();
  50.  
  51.   for( int j=0; j<rs.AvalInputs.GetCount(); j++ )
  52.     pExtensions += rs.AvalInputs[j].sExtensions;
  53.   
  54.   pFilter = "Supported media files (";
  55.   pFilter += pExtensions;
  56.   pFilter += ")";
  57.   
  58.   pFilter.insert(pFilter.end(), 0);
  59.   
  60.   pFilter += pExtensions;
  61.   pFilter.insert(pFilter.end(), 0);
  62.   
  63.   // All file types
  64.   for( j=0; j<rs.AvalInputs.GetCount(); j++ )
  65.   {
  66.     pFilter += rs.AvalInputs[j].sDescription;
  67.     pFilter += string(" (") + string(rs.AvalInputs[j].sExtensions) + string(")");
  68.     pFilter.insert(pFilter.end(), 0);
  69.     pFilter += rs.AvalInputs[j].sExtensions;
  70.     pFilter.insert(pFilter.end(), 0);
  71.   }
  72.   
  73.   pFilter += "All";
  74.   
  75.   pFilter.insert(pFilter.end(), 0);
  76.   pFilter += "*.*";
  77.   
  78.   pFilter.insert(pFilter.end(), 0);
  79.  
  80.   return pFilter.c_str(); 
  81. }
  82.  
  83. bool OpenFileDialog(char *inputFileName)
  84. {
  85.     OPENFILENAME oifn;
  86.   char     InputFileBuffer[MAXFILENAME];
  87.  
  88.   // Initialize OPENFILENAME
  89.   ZeroMemory(&oifn, sizeof(OPENFILENAME));
  90.   oifn.lStructSize = sizeof(OPENFILENAME);
  91.   oifn.hwndOwner = hMainWnd;
  92.   strcpy(InputFileBuffer,"");
  93.   oifn.lpstrFile = inputFileName;
  94.   oifn.nMaxFile = MAXFILENAME;
  95.   oifn.lpstrFilter = GetFilterString();
  96.   oifn.nFilterIndex = 1;
  97.   oifn.lpstrFileTitle = "FlasKMPEG - Choose input stream file";
  98.   oifn.nMaxFileTitle = 0;
  99.   oifn.lpstrInitialDir = rs.conf.inputDir;
  100.   oifn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  101.   if(!GetOpenFileName(&oifn))
  102.         return false;
  103.  
  104.   return true;
  105. }
  106.  
  107. int OpenFile(char *inputFileName)
  108. {
  109.  
  110.   if( !inputFileName )
  111.     return 0;
  112.  
  113.   // Disable menu. Enable it if everything went ok.
  114.     DisableMenu();
  115.  
  116.   // First close streams if opened
  117.   if( rs.pAudioMism.handle )
  118.     rs.pAudioMism.pMismInfo->CloseStream( rs.pAudioMism.handle );
  119.   if( rs.pVideoMism.handle )
  120.     rs.pVideoMism.pMismInfo->CloseStream( rs.pVideoMism.handle );
  121.   
  122.   // and reset the handles
  123.   rs.pAudioMism.handle = NULL;
  124.   rs.pVideoMism.handle = NULL;
  125.  
  126.   // Try to find a mism that accepts this file
  127.   int selectedMism = -1;
  128.  
  129.   DWORD nMerit, nMaxMerit = 0;
  130.   // Pick the one with highest merit
  131.   for( int i=0; i<rs.AvalInputs.GetCount(); i++ )
  132.   {
  133.     if (rs.AvalInputs[i].CanOpenFile( inputFileName, &nMerit ) )
  134.     {
  135.       if(nMerit>=nMaxMerit)
  136.       {
  137.         selectedMism = i;
  138.         nMaxMerit = nMerit;
  139.       }
  140.     }
  141.   }
  142.  
  143.   if( selectedMism < 0 )
  144.     return 0;
  145.  
  146.   // Now set the Misms for audio and video
  147.   rs.pVideoMism.pMismInfo = &rs.AvalInputs[selectedMism];
  148.   rs.pAudioMism.pMismInfo = &rs.AvalInputs[selectedMism];
  149.  
  150.   // Delete current audio and video decoders
  151.   //  if any
  152.   if(rs.audio){
  153.       delete rs.audio;
  154.       rs.audio=NULL;
  155.   }
  156.   if(rs.video){
  157.       delete rs.video;
  158.       rs.video=NULL;
  159.   }
  160.  
  161.   fmStreamIds Ids;
  162.   Ids.idCount = 0;
  163.     //Now, we have to get the streams available in
  164.   // this file.
  165.   // Here it likely that the user will be presented 
  166.   //   with a window to select streams.
  167.   // That is handled by the Mism.
  168.   // 
  169.   // Use avalInput to get the streams.
  170.   // Let's go.
  171.   rs.AvalInputs[selectedMism].GetStreams( &Ids , inputFileName );
  172.   if( !Ids.idCount )
  173.   {
  174.     PrintError(UNS_FILE_OPEN,(int)hMainWnd, 0);
  175.     return 0;
  176.   }
  177.  
  178.   // Store iDs in rs
  179.   rs.sStreamIds = Ids;
  180.  
  181.   //After recognizing type, you've obtained stream and substream
  182.     //available
  183.   fmStreamId videoId, audioId, spId;
  184.   if( !GetFirstVideoId( &Ids, &videoId ) )
  185.   {
  186.       PrintError(NO_VIDEO_TRACK,(int)hMainWnd, 0);
  187.       return 0;
  188.   }
  189.   // We got a video ID
  190.   
  191.   // And open the stream for the new working Mism
  192.   if( !(rs.pVideoMism.handle = rs.pVideoMism.pMismInfo->OpenStream( inputFileName ) ))
  193.       return 0;
  194.   
  195.   // Create video decoder
  196.   rs.video = new VideoWrapper();
  197.   
  198.   TVideoInit sVideoInit;
  199.   sVideoInit.pMismInfo          = &rs.pVideoMism;
  200.   if(rs.pVideoMism.pMismInfo->dwReserved)
  201.     sVideoInit.clut               = ((TExtraInfo *)rs.pVideoMism.pMismInfo->dwReserved)->clut;
  202.   else
  203.     sVideoInit.clut = 0;
  204.   sVideoInit.nStreamId          = videoId.streamId;
  205.   sVideoInit.nSubStreamId       = videoId.subStreamId;
  206.   sVideoInit.nSubpicStreamId    = GetFirstSubpicId(&Ids, &spId) ? spId.streamId : -1;
  207.   sVideoInit.nSubpicSubstreamId = GetFirstSubpicId(&Ids, &spId) ? spId.subStreamId : -1;
  208.   sVideoInit.ProgramDirectory   = program_directory;
  209.  
  210.   // Initialize it. From now on, the video decoder will take full control of
  211.   // this instance of the mism.
  212.   if(!rs.video->Init(&sVideoInit))
  213.   {
  214.       delete rs.video;
  215.       rs.video = NULL;
  216.       return 0;
  217.   }
  218.  
  219.   // If we have audio
  220.   int nIdx;
  221.   if( GetFirstAudioId( &Ids, &audioId, &nIdx ) )
  222.   {
  223.     // If we have audio, the default one is indicated
  224.     rs.sAudioTrack.nStreamId     = Ids.vIds[Ids.idAudioDef].streamId;
  225.     rs.sAudioTrack.nSubStreamId  = Ids.vIds[Ids.idAudioDef].subStreamId;
  226.  
  227.     // And open the stream for the new working Mism
  228.     if( ! (rs.pAudioMism.handle = rs.pAudioMism.pMismInfo->OpenStream( inputFileName )) )
  229.       return 0;
  230.     rs.audio = new Audio();
  231.     if(!rs.audio->Init(&rs.pAudioMism))
  232.     {
  233.       delete rs.audio; 
  234.       rs.audio=NULL;
  235.     }  
  236.   }
  237.   else
  238.     rs.audio = NULL;
  239.     
  240.  
  241.   TProfile defprof;
  242.  
  243.   defprof = *rs.profiler->GetSelected();
  244.  
  245.   if(rs.audio)
  246.   {
  247.       if( rs.audio->GetFormat()!=Ac3  &&  rs.audio->GetFormat()!=MpegAudio )
  248.       defprof.audioMode=NO_AUDIO;
  249.   }
  250.   else
  251.     defprof.audioMode=NO_AUDIO;
  252.  
  253.   // Modify profile values
  254.   TMPGVideoInfo *pVideoInfo = rs.video->GetVideoInfo();
  255.    
  256.   defprof.timeBase.scale = 
  257.      frame_rate_scale_table[pVideoInfo->detected_frame_rate_code];
  258.    
  259.   defprof.timeBase.sampleSize = 
  260.      frame_rate_scale_samplesize[pVideoInfo->detected_frame_rate_code];
  261.    
  262.   if( pVideoInfo->detected_frame_rate_code == 1  &&
  263.       pVideoInfo->frame_rate_code == 4 )
  264.     defprof.recons_progressive = true;
  265.  
  266.   rs.profiler->AddNSelect( &defprof );
  267.   
  268.   // Select the appropiate iDCT
  269.   if(rs.conf.idctAutoSelect)
  270.     // In case of auto select
  271.     rs.conf.idctIndex = rs.video->GetIdctSelectedIndex();  
  272.   else
  273.     // SelectIdct returns the index of the Idct actually selected
  274.     rs.conf.idctIndex  = rs.video->SelectIdct(rs.conf.idctIndex);
  275.  
  276.  
  277.   // Reset start file position
  278.     rs.startFilePos = 0;
  279.  
  280.   // Copy opened file name to run state variable
  281.   strcpy( rs.openedFileName, inputFileName );
  282.  
  283.   //No error enable MENU
  284.     EnableMenu();
  285.   return 1;
  286. }
  287.  
  288.