1 /*****************************************************************************
3 *****************************************************************************
5 * $Id: wxHB.cpp,v 1.8 2005/03/26 23:04:17 titer Exp $
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
31 /****************************************************************************
32 * Definitions / Variables
33 ****************************************************************************/
34 class hbApp *g_hbApp = NULL;
36 IMPLEMENT_APP( hbApp )
38 /****************************************************************************
40 ****************************************************************************/
41 class hbAppTimer: public wxTimer
46 Start( 50, wxTIMER_CONTINUOUS );
54 class hbAppProgress: public wxDialog
57 hbAppProgress( wxWindow *parent, wxString title, wxString msg ) :
58 wxDialog( parent, -1, title, wxDefaultPosition, wxSize( 250, 300),
59 wxDEFAULT_DIALOG_STYLE )
62 txt = new wxStaticText( this, -1, msg );
63 gauge = new wxGauge( this, -1, 100 );
64 button = new wxButton( this, wxID_CANCEL, wxU("Cancel") );
66 /* Add evrything in a sizer */
67 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
68 sizer->Add( txt, 1, wxEXPAND|wxALIGN_CENTER|wxALL, 5 );
70 wxBoxSizer *sizerH = new wxBoxSizer( wxHORIZONTAL );
71 sizerH->Add( gauge, 1, wxALIGN_CENTER|wxALL, 5 );
72 sizer->Add( sizerH, 0, wxALIGN_CENTER|wxALL, 5 );
74 sizer->Add( button, 0, wxALIGN_CENTER_HORIZONTAL|
75 wxALIGN_BOTTOM|wxALL, 5 );
76 //SetSizerAndFit( sizer );
80 void SetProgress( int i_percent, wxString msg )
82 gauge->SetValue( i_percent );
86 void OnClose( wxCloseEvent &event )
88 EndModal( wxID_CANCEL );
99 BEGIN_EVENT_TABLE( hbAppProgress, wxDialog )
100 EVT_CLOSE( hbAppProgress::OnClose )
103 /****************************************************************************
105 ****************************************************************************/
107 /* OnInit: Call at the very start and put every in place
115 /* Init all variables */
133 /* Start out timer */
134 timer = new hbAppTimer();
136 /* Create and Start the wizard */
137 wizard = new hbWizard( NULL );
140 /* Special hack FIXME */
156 /* Delete others FIXME */
165 /* Create a hb instance */
166 hbHandle = hb_init( HB_DEBUG_NONE, 1 );
167 if( hbHandle == NULL )
174 int hbApp::Scan( wxString sDevice )
176 if( sDevice.IsEmpty() )
181 if( titles ) delete titles; titles = NULL;
182 if( audios ) delete audios; audios = NULL;
183 if( subs ) delete subs; subs = NULL;
186 /* Append a \ if needed */
187 if( sDevice.Length() == 2 && sDevice.Last() == ':' )
188 sDevice.Append( '\\' );
190 /* Launch the scan (all titles) */
191 hb_scan( hbHandle, sDevice.ToAscii(), 0 );
193 /* Create a progress report */
194 progress = new hbAppProgress( wizard, wxU("Scanning..."), wxU("Opening ")+sDevice);
195 progress->ShowModal();
197 if( !hb_list_count( hb_get_titles( hbHandle ) ) )
199 hbError( wizard, wxU("Scanning failed") );
203 /* FIXME test if the scan is complete */
214 /* Maybe check a few things like:
215 * - compatible muxer and codecs */
217 /* Start the encoding */
218 hb_add( hbHandle, title->job );
219 hb_start( hbHandle );
221 /* FIXME use the wizard instead */
222 /* Create a progress report */
223 progress = new hbAppProgress( wizard, wxU("Encoding..."), wxU("0%"));
224 progress->ShowModal();
226 hb_get_state( hbHandle, &s );
227 if( s.param.workdone.error != HB_ERROR_NONE )
229 hb_stop( hbHandle ); /* FIXME to a proper handling */
230 hbError( wizard, wxU("Encoding failed/stopped") );
242 hb_close( &hbHandle );
246 wxArrayString *hbApp::GetTitles()
255 /* Create the title list */
256 list = hb_get_titles( hbHandle );
258 i_count = hb_list_count( list );
262 titles = new wxArrayString();
263 for( i = 0; i < i_count; i++ )
265 hb_title_t *t = (hb_title_t*)hb_list_item( list, i );
266 wxString name = wxString::Format( wxU("%d - %d:%02d:%02d"),
268 t->hours, t->minutes, t->seconds );
275 void hbApp::SetTitle( int _i_title )
279 if( i_title == _i_title )
283 title = (hb_title_t*)hb_list_item( hb_get_titles( hbHandle ), i_title );
284 if( audios ) delete audios; audios = NULL;
285 if( subs ) delete subs; subs = NULL;
287 for( i = 0; i < 8; i++ )
288 title->job->audios[i] = -1;
289 title->job->subtitle = -1;
292 int hbApp::GetDefaultTitle()
296 int i_best_length = 0;
299 list = hb_get_titles( hbHandle );
300 for( i = 0; i < hb_list_count( list ); i++ )
302 hb_title_t *t = (hb_title_t*)hb_list_item( list, i );
303 int i_length = t->hours * 3600 + t->minutes*60 + t->seconds;
305 if( i_best < 0 || i_length > i_best_length )
308 i_best_length = i_length;
317 int hbApp::GetChaptersCount()
322 return hb_list_count( title->list_chapter );
325 void hbApp::SetChapterStart( int i_chapter )
327 title->job->chapter_start = i_chapter;
330 void hbApp::SetChapterEnd( int i_chapter )
332 title->job->chapter_end = i_chapter;
335 wxArrayString *hbApp::GetTracksAudio()
341 audios = new wxArrayString();
342 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
344 hb_audio_t *a = (hb_audio_t*)hb_list_item( title->list_audio, i );
346 audios->Add( wxU(a->lang) );
348 audios->Add( wxU("None") );
352 void hbApp::SetTrackAudio( int i_track, int i_pos )
354 if( i_pos >= 0 && i_pos < hb_list_count( title->list_audio ) )
355 title->job->audios[i_pos] = i_track;
357 title->job->audios[i_pos] = -1;
360 wxArrayString *hbApp::GetTracksSubtitle()
367 subs = new wxArrayString();
368 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
371 (hb_subtitle_t*)hb_list_item( title->list_subtitle, i );
373 subs->Add( wxU(s->lang) );
375 subs->Add( wxU("None") );
380 void hbApp::SetTrackSubtitle( int i_track )
382 if( i_track >= 0 && i_track < hb_list_count( title->list_subtitle ) )
383 title->job->subtitle = i_track;
385 title->job->subtitle = -1;
388 void hbApp::SetOutputFile( wxString sFile )
392 title->job->file = strdup( sFile.ToAscii() ); /* FIXME do we need strdup ? */
394 psz = strrchr( title->job->file, '.' );
396 title->job->mux = HB_MUX_AVI; /* By default, FIXME */
397 else if( !strcasecmp( psz, ".avi" ) )
398 title->job->mux = HB_MUX_AVI;
399 else if( !strcasecmp( psz, ".mp4" ) || !strcasecmp( psz, ".mov") )
400 title->job->mux = HB_MUX_MP4;
401 else if( !strcasecmp( psz, ".ogg" ) || !strcasecmp( psz, ".ogm") )
402 title->job->mux = HB_MUX_OGM;
404 title->job->mux = HB_MUX_AVI; /* By default */
406 /* Fix acodec value */
407 switch( title->job->mux )
410 title->job->acodec = HB_ACODEC_LAME;
413 title->job->acodec = HB_ACODEC_FAAC;
416 title->job->acodec = HB_ACODEC_VORBIS;
421 void hbApp::SetAudioBitrate( int i_bitrate )
423 int i_old = title->job->abitrate;
425 title->job->abitrate = i_bitrate;
427 if( title->job->vbitrate > 0 )
428 title->job->vbitrate = title->job->vbitrate + (i_old - i_bitrate);
431 void hbApp::SetTotalBitrate( int i_bitrate )
433 title->job->vbitrate = i_bitrate - title->job->abitrate;
435 if( title->job->vbitrate <= 0 )
436 title->job->vbitrate = 1;
439 void hbApp::SetTotalSize( int i_size )
444 void hbApp::SetVideoPass( int i_pass )
446 /* FIXME is 0 or 1 valid for 1 pass ? */
448 title->job->pass = 0;
449 else if( i_pass == 2 )
450 title->job->pass = 2;
453 void hbApp::SetVideoCodec( wxString sCodec )
455 if( sCodec.Lower() == wxU("ffmpeg") )
456 title->job->vcodec = HB_VCODEC_FFMPEG;
457 else if( sCodec.Lower() == wxU("xvid") )
458 title->job->vcodec = HB_VCODEC_XVID;
459 else if( sCodec.Lower() == wxU("x264") )
460 title->job->vcodec = HB_VCODEC_XVID;
463 void hbApp::SetVideoDeinterlace( bool b_deinterlace )
465 title->job->deinterlace = b_deinterlace ? 1 : 0;
468 void hbApp::SetPriority( int i_priority )
470 /* Doesn't work :(( */
474 case HB_PRIORITY_ABOVE_NORMAL:
475 i_priority = THREAD_PRIORITY_ABOVE_NORMAL;
477 case HB_PRIORITY_BELOW_NORMAL:
478 i_priority = THREAD_PRIORITY_BELOW_NORMAL;
480 case HB_PRIORITY_LOWEST:
481 i_priority = THREAD_PRIORITY_LOWEST;
483 case HB_PRIORITY_HIGHEST:
484 i_priority = THREAD_PRIORITY_HIGHEST;
487 case HB_PRIORITY_NORMAL:
489 i_priority = THREAD_PRIORITY_NORMAL;
492 ::SetThreadPriority( GetCurrentThread(), i_priority );
498 void hbApp::SetCpuCount( int i_cpu )
500 hb_set_cpu_count( hbHandle, i_cpu );
503 int hbApp::GetVideoRateBase()
505 return title->rate_base;
508 void hbApp::SetVideoRateBase( int i_base )
510 title->job->vrate_base = i_base;
511 title->job->vrate = HB_VIDEO_RATE_BASE;
513 void hbApp::GetVideoAutocrop( int crop[4] )
516 for( i = 0; i < 4; i++ )
517 crop[i] = title->crop[i];
519 void hbApp::SetVideoCrop( int crop[4] )
522 for( i = 0; i < 4; i++ )
523 title->job->crop[i] = crop ? crop[i] : 0;
525 void hbApp::GetVideoSize( int *pi_width, int *pi_height )
527 *pi_width = title->width;
528 *pi_height = title->height;
530 void hbApp::SetVideoSize( int i_width, int i_height )
532 title->job->width = i_width;
533 title->job->height = i_height;
537 int hbApp::GetAudioSamplerate()
539 int idx = title->job->audios[0];
540 if( idx >= 0 && idx < hb_list_count( title->list_audio ) )
542 hb_audio_t *a = (hb_audio_t*)hb_list_item( title->list_audio, 0 );
547 return title->job->arate;
550 void hbApp::SetAudioSamplerate( int i_rate )
552 title->job->arate = i_rate;
555 wxString hbApp::GetDefaultAudio()
557 return wxU("English");
560 wxString hbApp::GetDefaultSubtitle()
565 int hbApp::GetDefaultCpuCount()
567 return GetSystemCpuCount();
570 int hbApp::GetDefaultPriority()
572 return HB_PRIORITY_NORMAL;
575 int hbApp::GetDefaultVideoRateBase()
580 int hbApp::GetDefaultAudioSamplerate()
586 wxArrayString *hbApp::GetSystemDrive()
591 systemDrive = new wxArrayString();
594 for( c = 'A'; c <= 'Z'; c++ )
602 if( GetDriveType( pszDrive ) == DRIVE_CDROM )
603 systemDrive->Add( wxString::Format( wxU("%c:"), c ) );
606 /* TODO true detection */
607 systemDrive->Add( wxU("/dev/dvd") );
608 systemDrive->Add( wxU("/dev/cdrom") );
614 int hbApp::GetSystemCpuCount()
616 return hb_get_cpu_count();
623 /* Special hack FIXME */
626 g_hbApp->ExitMainLoop();
631 hb_get_state( hbHandle, &s );
637 case HB_STATE_SCANNING:
639 int i_cur = s.param.scanning.title_cur;
640 int i_cnt = s.param.scanning.title_count;
644 progress->SetProgress( 100*(i_cur-1)/i_cnt,
645 wxString::Format(wxU("Scanning title %d of %d."),
651 case HB_STATE_SCANDONE:
654 progress->SetProgress( 100, wxU("Scanning complete.") );
655 progress->Close( TRUE );
661 case HB_STATE_WORKING:
663 float f_progress = s.param.working.progress;
664 float f_rate_cur = s.param.working.rate_cur;
665 float f_rate_avg = s.param.working.rate_avg;
667 progress->SetProgress( (int)(100 * f_progress),
668 wxString::Format(wxU("Encoding: %.2f %% (%.2f fps, avg %.2f fps)\n"),
669 100.0 * f_progress, f_rate_cur, f_rate_avg ));
673 case HB_STATE_WORKDONE:
676 progress->SetProgress( 100, wxU("Encoding complete.") );
677 progress->Close( TRUE );