OSDN Git Service

47b24edcd09dbf907ab5a2b5d30ef7300a595323
[handbrake-jp/handbrake-jp-git.git] / macosx / Controller.mm
1 /* $Id: Controller.mm,v 1.79 2005/11/04 19:41:32 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "Controller.h"
8 #include "a52dec/a52.h"
9 #import "HBOutputPanelController.h"
10 #import "HBPreferencesController.h"
11 /* Added to integrate scanning into HBController */
12 #include <IOKit/IOKitLib.h>
13 #include <IOKit/storage/IOMedia.h>
14 #include <IOKit/storage/IODVDMedia.h>
15 #include "HBDVDDetector.h"
16 #include "dvdread/dvd_reader.h"
17
18 #define _(a) NSLocalizedString(a,NULL)
19
20 static int FormatSettings[4][10] =
21   { { HB_MUX_MP4 | HB_VCODEC_FFMPEG | HB_ACODEC_FAAC,
22           HB_MUX_MP4 | HB_VCODEC_X264   | HB_ACODEC_FAAC,
23           0,
24           0 },
25     { HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_FAAC,
26           HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_AC3,
27           HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_LAME,
28           HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_VORBIS,
29           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_FAAC,
30           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_AC3,
31           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_LAME,
32           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_VORBIS,
33           0,
34           0 },
35     { HB_MUX_AVI | HB_VCODEC_FFMPEG | HB_ACODEC_LAME,
36           HB_MUX_AVI | HB_VCODEC_FFMPEG | HB_ACODEC_AC3,
37           HB_MUX_AVI | HB_VCODEC_X264   | HB_ACODEC_LAME,
38           HB_MUX_AVI | HB_VCODEC_X264   | HB_ACODEC_AC3},
39     { HB_MUX_OGM | HB_VCODEC_FFMPEG | HB_ACODEC_VORBIS,
40           HB_MUX_OGM | HB_VCODEC_FFMPEG | HB_ACODEC_LAME,
41           0,
42           0 } };
43
44 /* We setup the toolbar values here */
45 static NSString *        ToggleDrawerIdentifier             = @"Toggle Drawer Item Identifier";
46 static NSString *        StartEncodingIdentifier            = @"Start Encoding Item Identifier";
47 static NSString *        PauseEncodingIdentifier            = @"Pause Encoding Item Identifier";
48 static NSString *        ShowQueueIdentifier                = @"Show Queue Item Identifier";
49 static NSString *        AddToQueueIdentifier               = @"Add to Queue Item Identifier";
50 static NSString *        ShowActivityIdentifier             = @"Debug Output Item Identifier";
51 static NSString *        ChooseSourceIdentifier             = @"Choose Source Item Identifier";
52
53
54 /*******************************
55  * HBController implementation *
56  *******************************/
57 @implementation HBController
58
59 - init
60 {
61     self = [super init];
62     [HBPreferencesController registerUserDefaults];
63     fHandle = NULL;
64     /* Check for check for the app support directory here as
65         * outputPanel needs it right away, as may other future methods
66         */
67     /* We declare the default NSFileManager into fileManager */
68         NSFileManager * fileManager = [NSFileManager defaultManager];
69         /* we set the files and support paths here */
70         AppSupportDirectory = @"~/Library/Application Support/HandBrake";
71     AppSupportDirectory = [AppSupportDirectory stringByExpandingTildeInPath];
72     /* We check for the app support directory for handbrake */
73         if ([fileManager fileExistsAtPath:AppSupportDirectory] == 0) 
74         {
75                 // If it doesnt exist yet, we create it here 
76                 [fileManager createDirectoryAtPath:AppSupportDirectory attributes:nil];
77         }
78     
79     outputPanel = [[HBOutputPanelController alloc] init];
80     fPictureController = [[PictureController alloc] initWithDelegate:self];
81     fQueueController = [[HBQueueController alloc] init];
82     fAdvancedOptions = [[HBAdvancedController alloc] init];
83     fPreferencesController = [[HBPreferencesController alloc] init];
84     return self;
85 }
86
87
88 - (void) applicationDidFinishLaunching: (NSNotification *) notification
89 {
90     int    build;
91     char * version;
92     
93     // Init libhb
94         int debugLevel = [[NSUserDefaults standardUserDefaults] boolForKey:@"ShowVerboseOutput"] ? HB_DEBUG_ALL : HB_DEBUG_NONE;
95     fHandle = hb_init(debugLevel, [[NSUserDefaults standardUserDefaults] boolForKey:@"CheckForUpdates"]);
96     
97         // Set the Growl Delegate
98     [GrowlApplicationBridge setGrowlDelegate: self];    
99     /* Init others controllers */
100     [fPictureController SetHandle: fHandle];
101     [fQueueController   setHandle: fHandle];
102     [fQueueController   setHBController: self];
103         
104     fChapterTitlesDelegate = [[ChapterTitles alloc] init];
105     [fChapterTable setDataSource:fChapterTitlesDelegate];
106     
107     /* Call UpdateUI every 1/2 sec */
108     [[NSRunLoop currentRunLoop] addTimer: [NSTimer
109         scheduledTimerWithTimeInterval: 0.5 target: self
110                               selector: @selector( updateUI: ) userInfo: NULL repeats: YES]
111                                 forMode: NSEventTrackingRunLoopMode];
112     
113     // Open debug output window now if it was visible when HB was closed
114     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OutputPanelIsOpen"])
115         [self showDebugOutputPanel:nil];
116     
117     // Open queue window now if it was visible when HB was closed
118     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QueueWindowIsOpen"])
119         [self showQueueWindow:nil];
120     
121         [self openMainWindow:nil];
122     
123     if( ( build = hb_check_update( fHandle, &version ) ) > -1 )
124     {
125         /* Update available - tell the user */
126         
127         NSBeginInformationalAlertSheet( _( @"Update is available" ),
128                                         _( @"Go get it!" ), _( @"Discard" ), NULL, fWindow, self,
129                                         @selector( updateAlertDone:returnCode:contextInfo: ),
130                                         NULL, NULL, [NSString stringWithFormat:
131                                             _( @"HandBrake %s (build %d) is now available for download." ),
132                                             version, build] );
133         return;
134         
135     }
136         
137     /* Show Browse Sources Window ASAP */
138     [self performSelectorOnMainThread: @selector(browseSources:)
139                            withObject: NULL waitUntilDone: NO];
140                            }
141
142 - (void) updateAlertDone: (NSWindow *) sheet
143               returnCode: (int) returnCode contextInfo: (void *) contextInfo
144 {
145     if( returnCode == NSAlertDefaultReturn )
146     {
147         /* Go to HandBrake homepage and exit */
148         [self openHomepage: NULL];
149         [NSApp terminate: self];
150      
151     }
152     else
153     {
154          /* Show scan panel */
155         [self performSelectorOnMainThread: @selector(showScanPanel:)
156                                withObject: NULL waitUntilDone: NO];
157         return;
158                /* Go to HandBrake homepage and exit */
159         [self openHomepage: NULL];
160         [NSApp terminate: self];
161     }
162 }
163
164 - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) app
165 {
166     // Warn if encoding a movie
167     hb_state_t s;
168     hb_get_state( fHandle, &s );
169     hb_job_t * job = hb_current_job( fHandle );
170     if ( job && ( s.state != HB_STATE_IDLE ) )
171     {
172         hb_job_t * job = hb_current_job( fHandle );
173         int result = NSRunCriticalAlertPanel(
174                 NSLocalizedString(@"Are you sure you want to quit HandBrake?", nil),
175                 NSLocalizedString(@"%@ is currently encoding. If you quit HandBrake, your movie will be lost. Do you want to quit anyway?", nil),
176                 NSLocalizedString(@"Quit", nil), NSLocalizedString(@"Don't Quit", nil), nil,
177                 job ? [NSString stringWithUTF8String:job->title->name] : @"A movie" );
178         
179         if (result == NSAlertDefaultReturn)
180         {
181             [self doCancelCurrentJob];
182             return NSTerminateNow;
183         }
184         else
185             return NSTerminateCancel;
186     }
187     
188     // Warn if items still in the queue
189     else if ( hb_count( fHandle ) > 0 )
190     {
191         int result = NSRunCriticalAlertPanel(
192                 NSLocalizedString(@"Are you sure you want to quit HandBrake?", nil),
193                 NSLocalizedString(@"One or more encodes are queued for encoding. Do you want to quit anyway?", nil),
194                 NSLocalizedString(@"Quit", nil), NSLocalizedString(@"Don't Quit", nil), nil);
195         
196         if ( result == NSAlertDefaultReturn )
197             return NSTerminateNow;
198         else
199             return NSTerminateCancel;
200     }
201     
202     return NSTerminateNow;
203 }
204
205 - (void)applicationWillTerminate:(NSNotification *)aNotification
206 {
207         [outputPanel release];
208         [fQueueController release];
209         hb_close(&fHandle);
210 }
211
212
213 - (void) awakeFromNib
214 {
215     [fWindow center];
216     [fWindow setExcludedFromWindowsMenu:YES];
217     [fAdvancedOptions setView:fAdvancedView];
218
219     /* Initialize currentScanCount so HB can use it to
220                 evaluate successive scans */
221         currentScanCount = 0;
222         
223     /* Init UserPresets .plist */
224         [self loadPresets];
225                 
226         fRipIndicatorShown = NO;  // initially out of view in the nib
227         
228         /* Show/Dont Show Presets drawer upon launch based
229                 on user preference DefaultPresetsDrawerShow*/
230         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultPresetsDrawerShow"] > 0)
231         {
232                 [fPresetDrawer open];
233         }
234         
235         
236     /* Destination box*/
237     [fDstFormatPopUp removeAllItems];
238     [fDstFormatPopUp addItemWithTitle: _( @"MP4 file" )];
239         [fDstFormatPopUp addItemWithTitle: _( @"MKV file" )];
240     [fDstFormatPopUp addItemWithTitle: _( @"AVI file" )];
241     [fDstFormatPopUp addItemWithTitle: _( @"OGM file" )];
242     [fDstFormatPopUp selectItemAtIndex: 0];
243         
244     [self formatPopUpChanged: NULL];
245     
246         /* We enable the create chapters checkbox here since we are .mp4 */     
247         [fCreateChapterMarkers setEnabled: YES];
248         if ([fDstFormatPopUp indexOfSelectedItem] == 0 && [[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultChapterMarkers"] > 0)
249         {
250                 [fCreateChapterMarkers setState: NSOnState];
251         }
252         
253         
254         
255         
256     [fDstFile2Field setStringValue: [NSString stringWithFormat:
257         @"%@/Desktop/Movie.mp4", NSHomeDirectory()]];
258         
259     /* Video encoder */
260     [fVidEncoderPopUp removeAllItems];
261     [fVidEncoderPopUp addItemWithTitle: @"FFmpeg"];
262     [fVidEncoderPopUp addItemWithTitle: @"XviD"];
263         
264     
265         
266     /* Video quality */
267     [fVidTargetSizeField setIntValue: 700];
268         [fVidBitrateField    setIntValue: 1000];
269         
270     [fVidQualityMatrix   selectCell: fVidBitrateCell];
271     [self videoMatrixChanged: NULL];
272         
273     /* Video framerate */
274     [fVidRatePopUp removeAllItems];
275         [fVidRatePopUp addItemWithTitle: _( @"Same as source" )];
276     for( int i = 0; i < hb_video_rates_count; i++ )
277     {
278         if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%.3f",23.976]])
279                 {
280                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
281                                 [NSString stringWithCString: hb_video_rates[i].string], @" (NTSC Film)"]];
282                 }
283                 else if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%d",25]])
284                 {
285                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
286                                 [NSString stringWithCString: hb_video_rates[i].string], @" (PAL Film/Video)"]];
287                 }
288                 else if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%.2f",29.97]])
289                 {
290                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
291                                 [NSString stringWithCString: hb_video_rates[i].string], @" (NTSC Video)"]];
292                 }
293                 else
294                 {
295                         [fVidRatePopUp addItemWithTitle:
296                                 [NSString stringWithCString: hb_video_rates[i].string]];
297                 }
298     }
299     [fVidRatePopUp selectItemAtIndex: 0];
300         
301         /* Picture Settings */
302         [fPicLabelPAROutputX setStringValue: @""];
303         [fPicSettingPARWidth setStringValue: @""];
304         [fPicSettingPARHeight setStringValue:  @""];
305         
306         /* Set Auto Crop to On at launch */
307     [fPictureController setAutoCrop:YES];
308         
309         /* Audio bitrate */
310     [fAudBitratePopUp removeAllItems];
311     for( int i = 0; i < hb_audio_bitrates_count; i++ )
312     {
313         [fAudBitratePopUp addItemWithTitle:
314                                 [NSString stringWithCString: hb_audio_bitrates[i].string]];
315
316     }
317     [fAudBitratePopUp selectItemAtIndex: hb_audio_bitrates_default];
318         
319     /* Audio samplerate */
320     [fAudRatePopUp removeAllItems];
321     for( int i = 0; i < hb_audio_rates_count; i++ )
322     {
323         [fAudRatePopUp addItemWithTitle:
324             [NSString stringWithCString: hb_audio_rates[i].string]];
325     }
326     [fAudRatePopUp selectItemAtIndex: hb_audio_rates_default];
327         
328     /* Bottom */
329     [fStatusField setStringValue: @""];
330         
331     [self enableUI: NO];
332         [self setupToolbar];
333         
334         [fPresetsActionButton setMenu:fPresetsActionMenu];
335         
336         /* We disable the Turbo 1st pass checkbox since we are not x264 */
337         [fVidTurboPassCheck setEnabled: NO];
338         [fVidTurboPassCheck setState: NSOffState];
339         
340         
341         /* lets get our default prefs here */
342         [self getDefaultPresets: NULL];
343         /* lets initialize the current successful scancount here to 0 */
344         currentSuccessfulScanCount = 0;
345     
346 }
347
348 - (void) TranslateStrings
349 {
350     [fSrcTitleField     setStringValue: _( @"Title:" )];
351     [fSrcChapterField   setStringValue: _( @"Chapters:" )];
352     [fSrcChapterToField setStringValue: _( @"to" )];
353     [fSrcDuration1Field setStringValue: _( @"Duration:" )];
354
355     [fDstFormatField    setStringValue: _( @"Format:" )];
356     [fDstCodecsField    setStringValue: _( @"Codecs:" )];
357     [fDstFile1Field     setStringValue: _( @"File:" )];
358     [fDstBrowseButton   setTitle:       _( @"Browse" )];
359
360     [fVidRateField      setStringValue: _( @"Framerate (fps):" )];
361     [fVidEncoderField   setStringValue: _( @"Encoder:" )];
362     [fVidQualityField   setStringValue: _( @"Quality:" )];
363 }
364
365
366 - (void) enableUI: (bool) b
367 {
368     NSControl * controls[] =
369       { fSrcTitleField, fSrcTitlePopUp,
370         fSrcChapterField, fSrcChapterStartPopUp, fSrcChapterToField,
371         fSrcChapterEndPopUp, fSrcDuration1Field, fSrcDuration2Field,
372         fDstFormatField, fDstFormatPopUp, fDstCodecsField,
373         fDstCodecsPopUp, fDstFile1Field, fDstFile2Field,
374         fDstBrowseButton, fVidRateField, fVidRatePopUp,
375         fVidEncoderField, fVidEncoderPopUp, fVidQualityField,
376         fVidQualityMatrix, fVidGrayscaleCheck, fSubField, fSubPopUp,
377         fAudLang1Field, fAudLang1PopUp, fAudLang2Field, fAudLang2PopUp,
378         fAudTrack1MixLabel, fAudTrack1MixPopUp, fAudTrack2MixLabel, fAudTrack2MixPopUp,
379         fAudRateField, fAudRatePopUp, fAudBitrateField,
380         fAudBitratePopUp, fPictureButton,fQueueStatus, 
381                 fPicSrcWidth,fPicSrcHeight,fPicSettingWidth,fPicSettingHeight,fPicSettingARkeep,
382                 fPicSettingDeinterlace,fPicLabelSettings,fPicLabelSrc,fPicLabelOutp,
383                 fPicLabelAr,fPicLabelDeinterlace,fPicLabelSrcX,fPicLabelOutputX,
384                 fPicLabelPAROutputX,fPicSettingPARWidth,fPicSettingPARHeight,
385                 fPicSettingPAR,fPicLabelAnamorphic,tableView,fPresetsAdd,fPresetsDelete,
386                 fCreateChapterMarkers,fVidTurboPassCheck,fDstMpgLargeFileCheck,fPicLabelAutoCrop,
387                 fPicSettingAutoCrop,fPicSettingDetelecine,fPicLabelDetelecine,fPicLabelDenoise,fPicSettingDenoise,
388         fSubForcedCheck,fPicSettingDeblock,fPicLabelDeblock,};
389
390     for( unsigned i = 0;
391          i < sizeof( controls ) / sizeof( NSControl * ); i++ )
392     {
393         if( [[controls[i] className] isEqualToString: @"NSTextField"] )
394         {
395             NSTextField * tf = (NSTextField *) controls[i];
396             if( ![tf isBezeled] )
397             {
398                 [tf setTextColor: b ? [NSColor controlTextColor] :
399                     [NSColor disabledControlTextColor]];
400                 continue;
401             }
402         }
403         [controls[i] setEnabled: b];
404
405     }
406         
407         if (b) {
408
409         /* if we're enabling the interface, check if the audio mixdown controls need to be enabled or not */
410         /* these will have been enabled by the mass control enablement above anyway, so we're sense-checking it here */
411         [self setEnabledStateOfAudioMixdownControls: NULL];
412         
413         } else {
414
415                 [tableView setEnabled: NO];
416         
417         }
418
419     [self videoMatrixChanged: NULL];
420     [fAdvancedOptions enableUI:b];
421 }
422
423
424 /***********************************************************************
425  * UpdateDockIcon
426  ***********************************************************************
427  * Shows a progression bar on the dock icon, filled according to
428  * 'progress' (0.0 <= progress <= 1.0).
429  * Called with progress < 0.0 or progress > 1.0, restores the original
430  * icon.
431  **********************************************************************/
432 - (void) UpdateDockIcon: (float) progress
433 {
434     NSImage * icon;
435     NSData * tiff;
436     NSBitmapImageRep * bmp;
437     uint32_t * pen;
438     uint32_t black = htonl( 0x000000FF );
439     uint32_t red   = htonl( 0xFF0000FF );
440     uint32_t white = htonl( 0xFFFFFFFF );
441     int row_start, row_end;
442     int i, j;
443
444     /* Get application original icon */
445     icon = [NSImage imageNamed: @"NSApplicationIcon"];
446
447     if( progress < 0.0 || progress > 1.0 )
448     {
449         [NSApp setApplicationIconImage: icon];
450         return;
451     }
452
453     /* Get it in a raw bitmap form */
454     tiff = [icon TIFFRepresentationUsingCompression:
455             NSTIFFCompressionNone factor: 1.0];
456     bmp = [NSBitmapImageRep imageRepWithData: tiff];
457     
458     /* Draw the progression bar */
459     /* It's pretty simple (ugly?) now, but I'm no designer */
460
461     row_start = 3 * (int) [bmp size].height / 4;
462     row_end   = 7 * (int) [bmp size].height / 8;
463
464     for( i = row_start; i < row_start + 2; i++ )
465     {
466         pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
467         for( j = 0; j < (int) [bmp size].width; j++ )
468         {
469             pen[j] = black;
470         }
471     }
472     for( i = row_start + 2; i < row_end - 2; i++ )
473     {
474         pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
475         pen[0] = black;
476         pen[1] = black;
477         for( j = 2; j < (int) [bmp size].width - 2; j++ )
478         {
479             if( j < 2 + (int) ( ( [bmp size].width - 4.0 ) * progress ) )
480             {
481                 pen[j] = red;
482             }
483             else
484             {
485                 pen[j] = white;
486             }
487         }
488         pen[j]   = black;
489         pen[j+1] = black;
490     }
491     for( i = row_end - 2; i < row_end; i++ )
492     {
493         pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
494         for( j = 0; j < (int) [bmp size].width; j++ )
495         {
496             pen[j] = black;
497         }
498     }
499
500     /* Now update the dock icon */
501     tiff = [bmp TIFFRepresentationUsingCompression:
502             NSTIFFCompressionNone factor: 1.0];
503     icon = [[NSImage alloc] initWithData: tiff];
504     [NSApp setApplicationIconImage: icon];
505     [icon release];
506 }
507
508 - (void) updateUI: (NSTimer *) timer
509 {
510
511     hb_list_t  * list;
512     list = hb_get_titles( fHandle );    
513     /* check to see if there has been a new scan done
514         this bypasses the constraints of HB_STATE_WORKING
515         not allowing setting a newly scanned source */
516         int checkScanCount = hb_get_scancount( fHandle );
517         if (checkScanCount > currentScanCount)
518         {
519                 
520                 currentScanCount = checkScanCount;
521                 //[fScanController Cancel: NULL];
522                 [fScanIndicator setIndeterminate: NO];
523                 //[fScanIndicator setDoubleValue: 0.0];
524                 //[fScanIndicator setHidden: YES];
525
526                 [self showNewScan: NULL];
527         }
528         
529     hb_state_t s;
530     hb_get_state( fHandle, &s );
531         
532         
533     switch( s.state )
534     {
535         case HB_STATE_IDLE:
536                 break;
537 #define p s.param.scanning                      
538         case HB_STATE_SCANNING:
539                 {
540             
541             [fSrcDVD2Field setStringValue: [NSString stringWithFormat:
542                                             _( @"Scanning title %d of %d..." ),
543                                             p.title_cur, p.title_count]];
544             float scanprogress_total;
545             scanprogress_total = ( p.title_cur - 1 ) / p.title_count;
546             /* FIX ME: currently having an issue showing progress on the scan
547              * indicator, for now just set to barber pole.
548              */
549             //[fScanIndicator setHidden: NO];
550             [fScanIndicator setIndeterminate: YES];
551             //[fScanIndicator setDoubleValue: 100.0 * scanprogress_total];
552             break;
553                 }
554 #undef p
555         
556 #define p s.param.scandone
557         case HB_STATE_SCANDONE:
558         {
559                         
560                         [fScanIndicator setIndeterminate: NO];
561             [fScanIndicator setDoubleValue: 0.0];
562                         //[fScanIndicator setHidden: YES];
563                         [self showNewScan: NULL];
564             [toolbar validateVisibleItems];
565                         break;
566         }
567 #undef p
568                         
569 #define p s.param.working
570         case HB_STATE_WORKING:
571         {
572             float progress_total;
573             NSMutableString * string;
574                         /* Currently, p.job_cur and p.job_count get screwed up when adding
575                                 jobs during encoding, if they cannot be fixed in libhb, will implement a
576                                 nasty but working cocoa solution */
577                         /* Update text field */
578                         string = [NSMutableString stringWithFormat: _( @"Encoding: task %d of %d, %.2f %%" ), p.job_cur, p.job_count, 100.0 * p.progress];
579             
580                         if( p.seconds > -1 )
581             {
582                 [string appendFormat:
583                     _( @" (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)" ),
584                     p.rate_cur, p.rate_avg, p.hours, p.minutes, p.seconds];
585             }
586             [fStatusField setStringValue: string];
587                         
588             /* Update slider */
589                         progress_total = ( p.progress + p.job_cur - 1 ) / p.job_count;
590             [fRipIndicator setIndeterminate: NO];
591             [fRipIndicator setDoubleValue: 100.0 * progress_total];
592                         
593             // If progress bar hasn't been revealed at the bottom of the window, do
594             // that now. This code used to be in doRip. I moved it to here to handle
595             // the case where hb_start is called by HBQueueController and not from
596             // HBController.
597             if (!fRipIndicatorShown)
598             {
599                 NSRect frame = [fWindow frame];
600                 if (frame.size.width <= 591)
601                     frame.size.width = 591;
602                 frame.size.height += 36;
603                 frame.origin.y -= 36;
604                 [fWindow setFrame:frame display:YES animate:YES];
605                 fRipIndicatorShown = YES;
606                 /* We check to see if we need to warn the user that the computer will go to sleep
607                    or shut down when encoding is finished */
608                 [self remindUserOfSleepOrShutdown];
609             }
610
611             /* Update dock icon */
612             [self UpdateDockIcon: progress_total];
613                         
614             // Has current job changed? That means the queue has probably changed as
615                         // well so update it
616             [fQueueController hblibStateChanged: s];
617             
618             break;
619         }
620 #undef p
621                         
622 #define p s.param.muxing
623         case HB_STATE_MUXING:
624         {
625             NSMutableString * string;
626                         
627             /* Update text field */
628             string = [NSMutableString stringWithFormat:
629                 _( @"Muxing..." )];
630             [fStatusField setStringValue: string];
631                         
632             /* Update slider */
633             [fRipIndicator setIndeterminate: YES];
634             [fRipIndicator startAnimation: nil];
635                         
636             /* Update dock icon */
637             [self UpdateDockIcon: 1.0];
638                         
639                         // Pass along the info to HBQueueController
640             [fQueueController hblibStateChanged: s];
641                         
642             break;
643         }
644 #undef p
645                         
646         case HB_STATE_PAUSED:
647                     [fStatusField setStringValue: _( @"Paused" )];
648             
649                         // Pass along the info to HBQueueController
650             [fQueueController hblibStateChanged: s];
651
652             break;
653                         
654         case HB_STATE_WORKDONE:
655         {
656             // HB_STATE_WORKDONE happpens as a result of hblib finishing all its jobs
657             // or someone calling hb_stop. In the latter case, hb_stop does not clear
658             // out the remaining passes/jobs in the queue. We'll do that here.
659                         
660             // Delete all remaining scans of this job, ie, delete whole encodes.
661             hb_job_t * job;
662             while( ( job = hb_job( fHandle, 0 ) ) && (job->sequence_id != 0) )
663                 hb_rem( fHandle, job );
664
665             // Start processing back up if jobs still left in queue
666             if (hb_count(fHandle) > 0)
667             {
668                 hb_start(fHandle);
669                 fEncodeState = 1;
670                 // Validate the toolbar (hack). The toolbar will usually get autovalidated
671                 // before we had the chance to restart the queue, hence it will now be in
672                 // the wrong state.
673                 [toolbar validateVisibleItems];
674                 break;
675             }
676
677             [fStatusField setStringValue: _( @"Done." )];
678             [fRipIndicator setIndeterminate: NO];
679             [fRipIndicator setDoubleValue: 0.0];
680             [toolbar validateVisibleItems];
681
682             /* Restore dock icon */
683             [self UpdateDockIcon: -1.0];
684
685             if (fRipIndicatorShown)
686             {
687                 NSRect frame = [fWindow frame];
688                 if (frame.size.width <= 591)
689                                     frame.size.width = 591;
690                 frame.size.height += -36;
691                 frame.origin.y -= -36;
692                 [fWindow setFrame:frame display:YES animate:YES];
693                                 fRipIndicatorShown = NO;
694                         }
695                         
696                         // Pass along the info to HBQueueController
697             [fQueueController hblibStateChanged: s];
698                         
699             /* Check to see if the encode state has not been cancelled
700                                 to determine if we should check for encode done notifications */
701                         if (fEncodeState != 2)                  {
702                                 /* If Growl Notification or Window and Growl has been selected */
703                                 if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Growl Notification"] || 
704                                         [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"])
705                 {
706                                         /*Growl Notification*/
707                                         [self showGrowlDoneNotification: NULL];
708                 }
709                 /* If Alert Window or Window and Growl has been selected */
710                                 if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window"] || 
711                                         [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"])
712                 {
713                                         /*On Screen Notification*/
714                                         int status;
715                                         NSBeep();
716                                         status = NSRunAlertPanel(@"Put down that cocktail...",@"Your HandBrake encode is done!", @"OK", nil, nil);
717                                         [NSApp requestUserAttention:NSCriticalRequest];
718                                         if ( status == NSAlertDefaultReturn ) 
719                                         {
720                                                 [self enableUI: YES];
721                                         }
722                 }
723                                 else
724                                 {
725                                         [self enableUI: YES];
726                                 }
727                                    /* If sleep has been selected */ 
728             if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Put Computer To Sleep"]) 
729                 { 
730                /* Sleep */ 
731                NSDictionary* errorDict; 
732                NSAppleEventDescriptor* returnDescriptor = NULL; 
733                NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: 
734                         @"tell application \"Finder\" to sleep"]; 
735                returnDescriptor = [scriptObject executeAndReturnError: &errorDict]; 
736                [scriptObject release]; 
737                [self enableUI: YES]; 
738                 } 
739             /* If Shutdown has been selected */ 
740             if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Shut Down Computer"]) 
741                 { 
742                /* Shut Down */ 
743                NSDictionary* errorDict; 
744                NSAppleEventDescriptor* returnDescriptor = NULL; 
745                NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: 
746                         @"tell application \"Finder\" to shut down"]; 
747                returnDescriptor = [scriptObject executeAndReturnError: &errorDict]; 
748                [scriptObject release]; 
749                [self enableUI: YES]; 
750                 }
751                         
752                                                 // MetaX insertion via AppleScript
753                         if([[NSUserDefaults standardUserDefaults] boolForKey: @"sendToMetaX"] == YES)
754                         {
755                         NSAppleScript *myScript = [[NSAppleScript alloc] initWithSource: [NSString stringWithFormat: @"%@%@%@", @"tell application \"MetaX\" to open (POSIX file \"", [fDstFile2Field stringValue], @"\")"]];
756                         [myScript executeAndReturnError: nil];
757                         [myScript release];
758                         }
759                         
760                         
761                         }
762                         else
763                         {
764                                 [self enableUI: YES];
765                         }
766             break;
767         }
768     }
769         
770     /* Lets show the queue status here in the main window */
771         int queue_count = hb_count( fHandle );
772         if( queue_count )
773         {
774                 [fQueueStatus setStringValue: [NSString stringWithFormat:
775                         @"%d pass%s in the queue",
776                                                  queue_count, ( queue_count > 1 ) ? "es" : ""]];
777         }
778         else
779         {
780                 [fQueueStatus setStringValue: @""];
781         }
782 }
783
784
785 #pragma mark -
786 #pragma mark Toolbar
787 // ============================================================
788 // NSToolbar Related Methods
789 // ============================================================
790
791 - (void) setupToolbar {
792     toolbar = [[[NSToolbar alloc] initWithIdentifier: @"HandBrake Toolbar"] autorelease];
793     
794     [toolbar setAllowsUserCustomization: YES];
795     [toolbar setAutosavesConfiguration: YES];
796     [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
797     
798     [toolbar setDelegate: self];
799     
800     [fWindow setToolbar: toolbar];
801 }
802
803 - (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier:
804     (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
805     NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdent];
806     
807     if ([itemIdent isEqualToString: ToggleDrawerIdentifier])
808     {
809         [item setLabel: @"Toggle Presets"];
810         [item setPaletteLabel: @"Toggler Presets"];
811         [item setToolTip: @"Open/Close Preset Drawer"];
812         [item setImage: [NSImage imageNamed: @"Drawer"]];
813         [item setTarget: self];
814         [item setAction: @selector(toggleDrawer:)];
815         [item setAutovalidates: NO];
816     }
817     else if ([itemIdent isEqualToString: StartEncodingIdentifier])
818     {
819         [item setLabel: @"Start"];
820         [item setPaletteLabel: @"Start Encoding"];
821         [item setToolTip: @"Start Encoding"];
822         [item setImage: [NSImage imageNamed: @"Play"]];
823         [item setTarget: self];
824         [item setAction: @selector(Rip:)];
825     }
826     else if ([itemIdent isEqualToString: ShowQueueIdentifier])
827     {
828         [item setLabel: @"Show Queue"];
829         [item setPaletteLabel: @"Show Queue"];
830         [item setToolTip: @"Show Queue"];
831         [item setImage: [NSImage imageNamed: @"Queue"]];
832         [item setTarget: self];
833         [item setAction: @selector(showQueueWindow:)];
834         [item setAutovalidates: NO];
835     }
836     else if ([itemIdent isEqualToString: AddToQueueIdentifier])
837     {
838         [item setLabel: @"Add to Queue"];
839         [item setPaletteLabel: @"Add to Queue"];
840         [item setToolTip: @"Add to Queue"];
841         [item setImage: [NSImage imageNamed: @"AddToQueue"]];
842         [item setTarget: self];
843         [item setAction: @selector(addToQueue:)];
844     }
845     else if ([itemIdent isEqualToString: PauseEncodingIdentifier])
846     {
847         [item setLabel: @"Pause"];
848         [item setPaletteLabel: @"Pause Encoding"];
849         [item setToolTip: @"Pause Encoding"];
850         [item setImage: [NSImage imageNamed: @"Pause"]];
851         [item setTarget: self];
852         [item setAction: @selector(Pause:)];
853     }
854     else if ([itemIdent isEqualToString: ShowActivityIdentifier]) {
855         [item setLabel: @"Activity Window"];
856         [item setPaletteLabel: @"Show Activity Window"];
857         [item setToolTip: @"Show Activity Window"];
858         [item setImage: [NSImage imageNamed: @"ActivityWindow"]];
859         [item setTarget: self];
860         [item setAction: @selector(showDebugOutputPanel:)];
861         [item setAutovalidates: NO];
862     }
863     else if ([itemIdent isEqualToString: ChooseSourceIdentifier])
864     {
865         [item setLabel: @"Source"];
866         [item setPaletteLabel: @"Source"];
867         [item setToolTip: @"Choose Video Source"];
868         [item setImage: [NSImage imageNamed: @"Source"]];
869         [item setTarget: self];
870         [item setAction: @selector(browseSources:)];
871     }
872     else
873     {
874         [item release];
875         return nil;
876     }
877
878     return item;
879 }
880
881 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
882 {
883     return [NSArray arrayWithObjects: ChooseSourceIdentifier, NSToolbarSeparatorItemIdentifier, StartEncodingIdentifier,
884         PauseEncodingIdentifier, AddToQueueIdentifier, ShowQueueIdentifier, NSToolbarFlexibleSpaceItemIdentifier, 
885                 NSToolbarSpaceItemIdentifier, ShowActivityIdentifier, ToggleDrawerIdentifier, nil];
886 }
887
888 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
889 {
890     return [NSArray arrayWithObjects:  StartEncodingIdentifier, PauseEncodingIdentifier, AddToQueueIdentifier,
891         ChooseSourceIdentifier, ShowQueueIdentifier, ShowActivityIdentifier, ToggleDrawerIdentifier,
892         NSToolbarCustomizeToolbarItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier,
893         NSToolbarSpaceItemIdentifier, NSToolbarSeparatorItemIdentifier, nil];
894 }
895
896 - (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem
897 {
898     NSString * ident = [toolbarItem itemIdentifier];
899         
900     if (fHandle)
901     {
902         hb_state_t s;
903         hb_get_state2( fHandle, &s );
904         
905         if (s.state == HB_STATE_WORKING || s.state == HB_STATE_MUXING)
906         {
907             if ([ident isEqualToString: StartEncodingIdentifier])
908             {
909                 [toolbarItem setImage: [NSImage imageNamed: @"Stop"]];
910                 [toolbarItem setLabel: @"Stop"];
911                 [toolbarItem setPaletteLabel: @"Stop"];
912                 [toolbarItem setToolTip: @"Stop Encoding"];
913                 return YES;
914             }
915             if ([ident isEqualToString: PauseEncodingIdentifier])
916             {
917                 [toolbarItem setImage: [NSImage imageNamed: @"Pause"]];
918                 [toolbarItem setLabel: @"Pause"];
919                 [toolbarItem setPaletteLabel: @"Pause Encoding"];
920                 [toolbarItem setToolTip: @"Pause Encoding"];
921                 return YES;
922             }
923             if (SuccessfulScan)
924                 if ([ident isEqualToString: AddToQueueIdentifier])
925                     return YES;
926         }
927         else if (s.state == HB_STATE_PAUSED)
928         {
929             if ([ident isEqualToString: PauseEncodingIdentifier])
930             {
931                 [toolbarItem setImage: [NSImage imageNamed: @"Play"]];
932                 [toolbarItem setLabel: @"Resume"];
933                 [toolbarItem setPaletteLabel: @"Resume Encoding"];
934                 [toolbarItem setToolTip: @"Resume Encoding"];
935                 return YES;
936             }
937             if ([ident isEqualToString: StartEncodingIdentifier])
938                 return YES;
939             if ([ident isEqualToString: AddToQueueIdentifier])
940                 return YES;
941         }
942         else if (s.state == HB_STATE_SCANNING)
943             return NO;
944         else if (s.state == HB_STATE_WORKDONE || s.state == HB_STATE_SCANDONE || SuccessfulScan)
945         {
946             if ([ident isEqualToString: StartEncodingIdentifier])
947             {
948                 [toolbarItem setImage: [NSImage imageNamed: @"Play"]];
949                 if (hb_count(fHandle) > 0)
950                     [toolbarItem setLabel: @"Start Queue"];
951                 else
952                     [toolbarItem setLabel: @"Start"];
953                 [toolbarItem setPaletteLabel: @"Start Encoding"];
954                 [toolbarItem setToolTip: @"Start Encoding"];
955                 return YES;
956             }
957             if ([ident isEqualToString: AddToQueueIdentifier])
958                 return YES;
959         }
960
961     }
962     
963     if ([ident isEqualToString: ShowQueueIdentifier])
964         return YES;
965     if ([ident isEqualToString: ToggleDrawerIdentifier])
966         return YES;
967     if ([ident isEqualToString: ChooseSourceIdentifier])
968         return YES;
969     if ([ident isEqualToString: ShowActivityIdentifier])
970         return YES;
971     
972     return NO;
973 }
974
975 - (BOOL) validateMenuItem: (NSMenuItem *) menuItem
976 {
977     SEL action = [menuItem action];
978     
979     hb_state_t s;
980     hb_get_state2( fHandle, &s );
981     
982     if (fHandle)
983     {
984         if (action == @selector(addToQueue:) || action == @selector(showPicturePanel:) || action == @selector(showAddPresetPanel:))
985             return SuccessfulScan && [fWindow attachedSheet] == nil;
986         
987         if (action == @selector(browseSources:))
988         {
989             if (s.state == HB_STATE_SCANNING)
990                 return NO;
991             else
992                 return [fWindow attachedSheet] == nil;
993         }
994         if (action == @selector(selectDefaultPreset:))
995             return [tableView selectedRow] >= 0 && [fWindow attachedSheet] == nil;
996         if (action == @selector(Pause:))
997         {
998             if (s.state == HB_STATE_WORKING)
999             {
1000                 if(![[menuItem title] isEqualToString:@"Pause Encoding"])
1001                     [menuItem setTitle:@"Pause Encoding"];
1002                 return YES;
1003             }
1004             else if (s.state == HB_STATE_PAUSED)
1005             {
1006                 if(![[menuItem title] isEqualToString:@"Resume Encoding"])
1007                     [menuItem setTitle:@"Resume Encoding"];
1008                 return YES;
1009             }
1010             else
1011                 return NO;
1012         }
1013         if (action == @selector(Rip:))
1014             if (s.state == HB_STATE_WORKING || s.state == HB_STATE_MUXING || s.state == HB_STATE_PAUSED)
1015             {
1016                 if(![[menuItem title] isEqualToString:@"Stop Encoding"])
1017                     [menuItem setTitle:@"Stop Encoding"];
1018                 return YES;
1019             }
1020             else if (SuccessfulScan)
1021             {
1022                 if(![[menuItem title] isEqualToString:@"Start Encoding"])
1023                     [menuItem setTitle:@"Start Encoding"];
1024                 return [fWindow attachedSheet] == nil;
1025             }
1026             else
1027                 return NO;
1028         }
1029     
1030     return YES;
1031 }
1032
1033 #pragma mark -
1034 #pragma mark Growl
1035 // register a test notification and make
1036 // it enabled by default
1037 #define SERVICE_NAME @"Encode Done"
1038 - (NSDictionary *)registrationDictionaryForGrowl 
1039
1040     NSDictionary *registrationDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
1041     [NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_ALL, 
1042     [NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_DEFAULT, 
1043     nil]; 
1044
1045     return registrationDictionary; 
1046
1047
1048 -(IBAction)showGrowlDoneNotification:(id)sender
1049 {
1050   [GrowlApplicationBridge 
1051             notifyWithTitle:@"Put down that cocktail..." 
1052                 description:@"your HandBrake encode is done!" 
1053            notificationName:SERVICE_NAME
1054                    iconData:nil 
1055                    priority:0 
1056                    isSticky:1 
1057                clickContext:nil];
1058 }
1059
1060 #pragma mark -
1061 #pragma mark Get New Source
1062
1063 /*Opens the source browse window, called from Open Source widgets */
1064 - (IBAction) browseSources: (id) sender
1065 {
1066     [self enableUI: NO];
1067     NSOpenPanel * panel;
1068         
1069     panel = [NSOpenPanel openPanel];
1070     [panel setAllowsMultipleSelection: NO];
1071     [panel setCanChooseFiles: YES];
1072     [panel setCanChooseDirectories: YES ];
1073     NSString * sourceDirectory;
1074         if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"])
1075         {
1076                 sourceDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"];
1077         }
1078         else
1079         {
1080                 sourceDirectory = @"~/Desktop";
1081                 sourceDirectory = [sourceDirectory stringByExpandingTildeInPath];
1082         }
1083     /* we open up the browse sources sheet here and call for browseSourcesDone after the sheet is closed
1084         * to evaluate whether we want to specify a title, we pass the sender in the contextInfo variable
1085         */
1086     [panel beginSheetForDirectory: sourceDirectory file: nil types: nil
1087                    modalForWindow: fWindow modalDelegate: self
1088                    didEndSelector: @selector( browseSourcesDone:returnCode:contextInfo: )
1089                       contextInfo: sender]; 
1090 }
1091
1092 - (void) browseSourcesDone: (NSOpenPanel *) sheet
1093                 returnCode: (int) returnCode contextInfo: (void *) contextInfo
1094 {
1095     /* we convert the sender content of contextInfo back into a variable called sender
1096     * mostly just for consistency for evaluation later
1097     */
1098     id sender = (id)contextInfo;
1099     /* User selected a file to open */
1100         if( returnCode == NSOKButton )
1101     {
1102         
1103         NSString *scanPath = [[sheet filenames] objectAtIndex: 0];
1104         /* we set the last searched source directory in the prefs here */
1105         NSString *sourceDirectory = [scanPath stringByDeletingLastPathComponent];
1106         [[NSUserDefaults standardUserDefaults] setObject:sourceDirectory forKey:@"LastSourceDirectory"];
1107         /* we order out sheet, which is the browse window as we need to open
1108             * the title selection sheet right away
1109             */
1110         [sheet orderOut: self];
1111         
1112         if (sender == fOpenSourceTitleMMenu)
1113         {
1114             /* We put the chosen source path in the source display text field for the
1115             * source title selection sheet in which the user specifies the specific title to be
1116             * scanned  as well as the short source name in fSrcDsplyNameTitleScan just for display
1117             * purposes in the title panel
1118             */
1119             /* Full Path */
1120             [fScanSrcTitlePathField setStringValue: [NSString stringWithFormat:@"%@", scanPath]];
1121             NSString *displayTitlescanSourceName;
1122             
1123             if ([[scanPath lastPathComponent] isEqualToString: @"VIDEO_TS"])
1124             {
1125                 /* If VIDEO_TS Folder is chosen, choose its parent folder for the source display name 
1126                 we have to use the title->dvd value so we get the proper name of the volume if a physical dvd is the source*/
1127                 displayTitlescanSourceName = [NSString stringWithFormat:[[scanPath stringByDeletingLastPathComponent] lastPathComponent]];
1128             }
1129             else
1130             {
1131                 /* if not the VIDEO_TS Folder, we can assume the chosen folder is the source name */
1132                 displayTitlescanSourceName = [NSString stringWithFormat:[scanPath lastPathComponent]];
1133             }
1134             /* we set the source display name in the title selection dialogue */
1135             [fSrcDsplyNameTitleScan setStringValue: [NSString stringWithFormat:@"%@", displayTitlescanSourceName]];
1136             /* We show the actual sheet where the user specifies the title to be scanned 
1137                 * as we are going to do a title specific scan
1138                 */
1139             [self showSourceTitleScanPanel:NULL];
1140         }
1141         else
1142         {
1143             /* We are just doing a standard full source scan, so we specify "0" to libhb */
1144             NSString *path = [[sheet filenames] objectAtIndex: 0];
1145             [self performScan:path scanTitleNum:0];   
1146         }
1147         
1148     }
1149     else // User clicked Cancel in browse window
1150     {
1151         /* if we have a title loaded up */
1152         if ([[fSrcDVD2Field stringValue] length] > 0)
1153         {
1154             [self enableUI: YES];
1155         }
1156     }
1157 }
1158
1159 /* Here we open the title selection sheet where we can specify an exact title to be scanned */
1160 - (IBAction) showSourceTitleScanPanel: (id) sender
1161 {
1162     /* We default the title number to be scanned to "0" which results in a full source scan, unless the
1163     * user changes it
1164     */
1165     [fScanSrcTitleNumField setStringValue: @"0"];
1166         /* Show the panel */
1167         [NSApp beginSheet: fScanSrcTitlePanel modalForWindow: fWindow modalDelegate: NULL didEndSelector: NULL contextInfo: NULL];
1168 }
1169
1170 - (IBAction) closeSourceTitleScanPanel: (id) sender
1171 {
1172     [NSApp endSheet: fScanSrcTitlePanel];
1173     [fScanSrcTitlePanel orderOut: self];
1174     
1175     
1176     
1177     if(sender == fScanSrcTitleOpenButton)
1178     {
1179         /* We setup the scan status in the main window to indicate a source title scan */
1180         [fSrcDVD2Field setStringValue: _( @"Opening a new source title ..." )];
1181                 //[fScanIndicator setHidden: NO];
1182             [fScanIndicator setIndeterminate: YES];
1183         [fScanIndicator startAnimation: nil];
1184                 
1185         /* We use the performScan method to actually perform the specified scan passing the path and the title
1186             * to be scanned
1187             */
1188         [self performScan:[fScanSrcTitlePathField stringValue] scanTitleNum:[fScanSrcTitleNumField intValue]];
1189     }
1190 }
1191
1192
1193 /* Here we actually tell hb_scan to perform the source scan, using the path to source and title number*/
1194 - (void) performScan:(NSString *) scanPath scanTitleNum: (int) scanTitleNum
1195 {
1196     NSString *path = scanPath;
1197     HBDVDDetector *detector = [HBDVDDetector detectorForPath:path];
1198     if( [detector isVideoDVD] )
1199     {
1200         // The chosen path was actually on a DVD, so use the raw block
1201         // device path instead.
1202         path = [detector devicePath];
1203     }
1204     /* If there is no title number passed to scan, we use "0"
1205         * which causes the default behavior of a full source scan
1206     */
1207     if (!scanTitleNum)
1208     {
1209         scanTitleNum = 0;
1210     }
1211     //[fScanIndicator setHidden: NO];
1212     hb_scan( fHandle, [path UTF8String], scanTitleNum );
1213     
1214 }
1215
1216 - (IBAction) showNewScan:(id)sender
1217 {
1218         hb_list_t  * list;
1219         hb_title_t * title;
1220         int indxpri=0;    // Used to search the longuest title (default in combobox)
1221         int longuestpri=0; // Used to search the longuest title (default in combobox)
1222         
1223         list = hb_get_titles( fHandle );
1224         
1225         if( !hb_list_count( list ) )
1226         {
1227                 /* We display a message if a valid dvd source was not chosen */
1228                 [fSrcDVD2Field setStringValue: @"No Valid Title Found"];
1229         SuccessfulScan = NO;
1230         }
1231         else
1232         {
1233         /* We increment the successful scancount here by one,
1234         which we use at the end of this function to tell the gui
1235         if this is the first successful scan since launch and whether
1236         or not we should set all settings to the defaults */
1237                 
1238         currentSuccessfulScanCount++;
1239         
1240         [toolbar validateVisibleItems];
1241                 
1242                 [fSrcTitlePopUp removeAllItems];
1243                 for( int i = 0; i < hb_list_count( list ); i++ )
1244                 {
1245                         title = (hb_title_t *) hb_list_item( list, i );
1246                         
1247             currentSource = [NSString stringWithUTF8String: title->name];
1248             
1249             /* To get the source name as well as the default output name, first we check to see if
1250                 the selected directory is the VIDEO_TS Directory */
1251             if ([[currentSource lastPathComponent] isEqualToString: @"VIDEO_TS"])
1252             {
1253                 /* If VIDEO_TS Folder is chosen, choose its parent folder for the source display name 
1254                 we have to use the title->dvd value so we get the proper name of the volume if a physical dvd is the source*/
1255                 sourceDisplayName = [NSString stringWithFormat:[[[NSString stringWithUTF8String: title->dvd] stringByDeletingLastPathComponent] lastPathComponent]];
1256             }
1257             else
1258             {
1259                 /* if not the VIDEO_TS Folder, we can assume the chosen folder is the source name */
1260                 sourceDisplayName = [NSString stringWithFormat:[currentSource lastPathComponent]];
1261             }
1262                         /*Set DVD Name at top of window*/
1263                         [fSrcDVD2Field setStringValue:[NSString stringWithFormat: @"%@", sourceDisplayName]];
1264                         
1265                         /* Use the dvd name in the default output field here 
1266                                 May want to add code to remove blank spaces for some dvd names*/
1267                         /* Check to see if the last destination has been set,use if so, if not, use Desktop */
1268                         if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastDestinationDirectory"])
1269                         {
1270                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1271                                         @"%@/%@.mp4", [[NSUserDefaults standardUserDefaults] stringForKey:@"LastDestinationDirectory"],sourceDisplayName]];
1272                         }
1273                         else
1274                         {
1275                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1276                                         @"%@/Desktop/%@.mp4", NSHomeDirectory(),sourceDisplayName]];
1277                         }
1278                         
1279                         if (longuestpri < title->hours*60*60 + title->minutes *60 + title->seconds)
1280                         {
1281                                 longuestpri=title->hours*60*60 + title->minutes *60 + title->seconds;
1282                                 indxpri=i;
1283                         }
1284                         
1285                         [self formatPopUpChanged:NULL];
1286                         
1287             [fSrcTitlePopUp addItemWithTitle: [NSString
1288                 stringWithFormat: @"%d - %02dh%02dm%02ds",
1289                 title->index, title->hours, title->minutes,
1290                 title->seconds]];
1291                 }
1292         
1293                 // Select the longuest title
1294                 [fSrcTitlePopUp selectItemAtIndex: indxpri];
1295                 [self titlePopUpChanged: NULL];
1296                 
1297         SuccessfulScan = YES;
1298                 [self enableUI: YES];
1299                 
1300                 /* if its the initial successful scan after awakeFromNib */
1301         if (currentSuccessfulScanCount == 1)
1302         {
1303             [self selectDefaultPreset: NULL];
1304             /* if Deinterlace upon launch is specified in the prefs, then set to 1 for "Fast",
1305             if not, then set to 0 for none */
1306             if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultDeinterlaceOn"] > 0)
1307                 [fPictureController setDeinterlace:1];
1308             else
1309                 [fPictureController setDeinterlace:0];
1310         }
1311         
1312         }
1313 }
1314
1315
1316 #pragma mark -
1317 #pragma mark New Output Destination
1318
1319 - (IBAction) browseFile: (id) sender
1320 {
1321     /* Open a panel to let the user choose and update the text field */
1322     NSSavePanel * panel = [NSSavePanel savePanel];
1323         /* We get the current file name and path from the destination field here */
1324         [panel beginSheetForDirectory: [[fDstFile2Field stringValue] stringByDeletingLastPathComponent] file: [[fDstFile2Field stringValue] lastPathComponent]
1325                                    modalForWindow: fWindow modalDelegate: self
1326                                    didEndSelector: @selector( browseFileDone:returnCode:contextInfo: )
1327                                           contextInfo: NULL];
1328 }
1329
1330 - (void) browseFileDone: (NSSavePanel *) sheet
1331     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1332 {
1333     if( returnCode == NSOKButton )
1334     {
1335         [fDstFile2Field setStringValue: [sheet filename]];
1336     }
1337 }
1338
1339
1340 #pragma mark -
1341 #pragma mark Main Window Control
1342
1343 - (IBAction) openMainWindow: (id) sender
1344 {
1345     [fWindow  makeKeyAndOrderFront:nil];
1346 }
1347
1348 - (BOOL) windowShouldClose: (id) sender
1349 {
1350     return YES;
1351 }
1352
1353 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
1354 {
1355     if( !flag ) {
1356         [fWindow  makeKeyAndOrderFront:nil];
1357                 
1358         return YES;
1359     }
1360     
1361     return NO;
1362 }
1363
1364 #pragma mark -
1365 #pragma mark Job Handling
1366
1367
1368 - (void) prepareJob
1369 {
1370     hb_list_t  * list  = hb_get_titles( fHandle );
1371     hb_title_t * title = (hb_title_t *) hb_list_item( list,
1372             [fSrcTitlePopUp indexOfSelectedItem] );
1373     hb_job_t * job = title->job;
1374     //int i;
1375
1376     /* Chapter selection */
1377     job->chapter_start = [fSrcChapterStartPopUp indexOfSelectedItem] + 1;
1378     job->chapter_end   = [fSrcChapterEndPopUp   indexOfSelectedItem] + 1;
1379         
1380     /* Format and codecs */
1381     int format = [fDstFormatPopUp indexOfSelectedItem];
1382     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
1383     job->mux    = FormatSettings[format][codecs] & HB_MUX_MASK;
1384     job->vcodec = FormatSettings[format][codecs] & HB_VCODEC_MASK;
1385     job->acodec = FormatSettings[format][codecs] & HB_ACODEC_MASK;
1386     /* If mpeg-4, then set mpeg-4 specific options like chapters and > 4gb file sizes */
1387         if ([fDstFormatPopUp indexOfSelectedItem] == 0)
1388         {
1389         /* We set the largeFileSize (64 bit formatting) variable here to allow for > 4gb files based on the format being
1390                 mpeg4 and the checkbox being checked 
1391                 *Note: this will break compatibility with some target devices like iPod, etc.!!!!*/
1392                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AllowLargeFiles"] > 0 && [fDstMpgLargeFileCheck state] == NSOnState)
1393                 {
1394                         job->largeFileSize = 1;
1395                 }
1396                 else
1397                 {
1398                         job->largeFileSize = 0;
1399                 }
1400         }
1401         if ([fDstFormatPopUp indexOfSelectedItem] == 0 || [fDstFormatPopUp indexOfSelectedItem] == 3)
1402         {
1403           /* We set the chapter marker extraction here based on the format being
1404                 mpeg4 or mkv and the checkbox being checked */
1405                 if ([fCreateChapterMarkers state] == NSOnState)
1406                 {
1407                         job->chapter_markers = 1;
1408                 }
1409                 else
1410                 {
1411                         job->chapter_markers = 0;
1412                 }
1413         }
1414         if( ( job->vcodec & HB_VCODEC_FFMPEG ) &&
1415         [fVidEncoderPopUp indexOfSelectedItem] > 0 )
1416     {
1417         job->vcodec = HB_VCODEC_XVID;
1418     }
1419     if( job->vcodec & HB_VCODEC_X264 )
1420     {
1421                 if ([fVidEncoderPopUp indexOfSelectedItem] > 0 )
1422             {
1423                         /* Just use new Baseline Level 3.0 
1424                         Lets Deprecate Baseline Level 1.3h264_level*/
1425                         job->h264_level = 30;
1426                         job->mux = HB_MUX_IPOD;
1427                         /* move sanity check for iPod Encoding here */
1428                         job->pixel_ratio = 0 ;
1429                         
1430                 }
1431                 
1432                 /* Set this flag to switch from Constant Quantizer(default) to Constant Rate Factor Thanks jbrjake
1433                 Currently only used with Constant Quality setting*/
1434                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultCrf"] > 0 && [fVidQualityMatrix selectedRow] == 2)
1435                 {
1436                 job->crf = 1;
1437                 }
1438                 
1439                 /* Below Sends x264 options to the core library if x264 is selected*/
1440                 /* Lets use this as per Nyx, Thanks Nyx!*/
1441                 job->x264opts = (char *)calloc(1024, 1); /* Fixme, this just leaks */
1442                 /* Turbo first pass if two pass and Turbo First pass is selected */
1443                 if( [fVidTwoPassCheck state] == NSOnState && [fVidTurboPassCheck state] == NSOnState )
1444                 {
1445                         /* pass the "Turbo" string to be appended to the existing x264 opts string into a variable for the first pass */
1446                         NSString *firstPassOptStringTurbo = @":ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0";
1447                         /* append the "Turbo" string variable to the existing opts string.
1448                         Note: the "Turbo" string must be appended, not prepended to work properly*/
1449                         NSString *firstPassOptStringCombined = [[fAdvancedOptions optionsString] stringByAppendingString:firstPassOptStringTurbo];
1450                         strcpy(job->x264opts, [firstPassOptStringCombined UTF8String]);
1451                 }
1452                 else
1453                 {
1454                         strcpy(job->x264opts, [[fAdvancedOptions optionsString] UTF8String]);
1455                 }
1456                 
1457         job->h264_13 = [fVidEncoderPopUp indexOfSelectedItem];
1458     }
1459
1460     /* Video settings */
1461     if( [fVidRatePopUp indexOfSelectedItem] > 0 )
1462     {
1463         job->vrate      = 27000000;
1464         job->vrate_base = hb_video_rates[[fVidRatePopUp
1465             indexOfSelectedItem]-1].rate;
1466     }
1467     else
1468     {
1469         job->vrate      = title->rate;
1470         job->vrate_base = title->rate_base;
1471     }
1472
1473     switch( [fVidQualityMatrix selectedRow] )
1474     {
1475         case 0:
1476             /* Target size.
1477                Bitrate should already have been calculated and displayed
1478                in fVidBitrateField, so let's just use it */
1479         case 1:
1480             job->vquality = -1.0;
1481             job->vbitrate = [fVidBitrateField intValue];
1482             break;
1483         case 2:
1484             job->vquality = [fVidQualitySlider floatValue];
1485             job->vbitrate = 0;
1486             break;
1487     }
1488
1489     job->grayscale = ( [fVidGrayscaleCheck state] == NSOnState );
1490
1491     /* Subtitle settings */
1492     job->subtitle = [fSubPopUp indexOfSelectedItem] - 2;
1493
1494     /* Audio tracks and mixdowns */
1495     /* check for the condition where track 2 has an audio selected, but track 1 does not */
1496     /* we will use track 2 as track 1 in this scenario */
1497     if ([fAudLang1PopUp indexOfSelectedItem] > 0)
1498     {
1499         job->audios[0] = [fAudLang1PopUp indexOfSelectedItem] - 1;
1500         job->audios[1] = [fAudLang2PopUp indexOfSelectedItem] - 1; /* will be -1 if "none" is selected */
1501         job->audios[2] = -1;
1502         job->audio_mixdowns[0] = [[fAudTrack1MixPopUp selectedItem] tag];
1503         job->audio_mixdowns[1] = [[fAudTrack2MixPopUp selectedItem] tag];
1504     }
1505     else if ([fAudLang2PopUp indexOfSelectedItem] > 0)
1506     {
1507         job->audios[0] = [fAudLang2PopUp indexOfSelectedItem] - 1;
1508         job->audio_mixdowns[0] = [[fAudTrack2MixPopUp selectedItem] tag];
1509         job->audios[1] = -1;
1510     }
1511     else
1512     {
1513         job->audios[0] = -1;
1514     }
1515
1516     /* Audio settings */
1517     job->arate = hb_audio_rates[[fAudRatePopUp
1518                      indexOfSelectedItem]].rate;
1519     job->abitrate = [[fAudBitratePopUp selectedItem] tag];
1520     
1521     /* set vfr according to the Picture Window */
1522     if ([fPictureController vfr])
1523     {
1524     job->vfr = 1;
1525     }
1526     else
1527     {
1528     job->vfr = 0;
1529     }
1530     
1531     /* Filters */ 
1532     job->filters = hb_list_init();
1533    
1534         /* Detelecine */
1535     if ([fPictureController detelecine])
1536     {
1537         hb_list_add( job->filters, &hb_filter_detelecine );
1538     }
1539    
1540     /* Deinterlace */
1541     if ([fPictureController deinterlace] == 1)
1542     {
1543         /* Run old deinterlacer by default */
1544         hb_filter_deinterlace.settings = "-1"; 
1545         hb_list_add( job->filters, &hb_filter_deinterlace );
1546     }
1547     else if ([fPictureController deinterlace] == 2)
1548     {
1549         /* Yadif mode 0 (1-pass with spatial deinterlacing.) */
1550         hb_filter_deinterlace.settings = "0"; 
1551         hb_list_add( job->filters, &hb_filter_deinterlace );            
1552     }
1553     else if ([fPictureController deinterlace] == 3)
1554     {
1555         /* Yadif (1-pass w/o spatial deinterlacing) and Mcdeint */
1556         hb_filter_deinterlace.settings = "2:-1:1"; 
1557         hb_list_add( job->filters, &hb_filter_deinterlace );            
1558     }
1559     else if ([fPictureController deinterlace] == 4)
1560     {
1561         /* Yadif (2-pass w/ spatial deinterlacing) and Mcdeint*/
1562         hb_filter_deinterlace.settings = "1:-1:1"; 
1563         hb_list_add( job->filters, &hb_filter_deinterlace );            
1564     }
1565         
1566         /* Denoise */
1567         
1568         if ([fPictureController denoise] == 1) // Weak in popup
1569         {
1570                 hb_filter_denoise.settings = "2:1:2:3"; 
1571         hb_list_add( job->filters, &hb_filter_denoise );        
1572         }
1573         else if ([fPictureController denoise] == 2) // Medium in popup
1574         {
1575                 hb_filter_denoise.settings = "3:2:2:3"; 
1576         hb_list_add( job->filters, &hb_filter_denoise );        
1577         }
1578         else if ([fPictureController denoise] == 3) // Strong in popup
1579         {
1580                 hb_filter_denoise.settings = "7:7:5:5"; 
1581         hb_list_add( job->filters, &hb_filter_denoise );        
1582         }
1583     
1584     /* Deblock  (uses pp7 default) */
1585     if ([fPictureController deblock])
1586     {
1587         hb_list_add( job->filters, &hb_filter_deblock );
1588     }
1589
1590 }
1591
1592
1593
1594 /* addToQueue: puts up an alert before ultimately calling doAddToQueue
1595 */
1596 - (IBAction) addToQueue: (id) sender
1597 {
1598         /* We get the destination directory from the destination field here */
1599         NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1600         /* We check for a valid destination here */
1601         if ([[NSFileManager defaultManager] fileExistsAtPath:destinationDirectory] == 0) 
1602         {
1603                 NSRunAlertPanel(@"Warning!", @"This is not a valid destination directory!", @"OK", nil, nil);
1604         return;
1605         }
1606
1607     /* We check for duplicate name here */
1608         if( [[NSFileManager defaultManager] fileExistsAtPath:
1609             [fDstFile2Field stringValue]] )
1610     {
1611         NSBeginCriticalAlertSheet( _( @"File already exists" ),
1612             _( @"Cancel" ), _( @"Overwrite" ), NULL, fWindow, self,
1613             @selector( overwriteAddToQueueAlertDone:returnCode:contextInfo: ),
1614             NULL, NULL, [NSString stringWithFormat:
1615             _( @"Do you want to overwrite %@?" ),
1616             [fDstFile2Field stringValue]] );
1617         // overwriteAddToQueueAlertDone: will be called when the alert is dismissed.
1618     }
1619     else
1620     {
1621         [self doAddToQueue];
1622     }
1623 }
1624
1625 /* overwriteAddToQueueAlertDone: called from the alert posted by addToQueue that asks
1626    the user if they want to overwrite an exiting movie file.
1627 */
1628 - (void) overwriteAddToQueueAlertDone: (NSWindow *) sheet
1629     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1630 {
1631     if( returnCode == NSAlertAlternateReturn )
1632         [self doAddToQueue];
1633 }
1634
1635 - (void) doAddToQueue
1636 {
1637     hb_list_t  * list  = hb_get_titles( fHandle );
1638     hb_title_t * title = (hb_title_t *) hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
1639     hb_job_t * job = title->job;
1640
1641     // Assign a sequence number, starting at zero, to each job added so they can
1642     // be lumped together in the UI.
1643     job->sequence_id = -1;
1644
1645     [self prepareJob];
1646
1647     /* Destination file */
1648     job->file = [[fDstFile2Field stringValue] UTF8String];
1649
1650     if( [fSubForcedCheck state] == NSOnState )
1651         job->subtitle_force = 1;
1652     else
1653         job->subtitle_force = 0;
1654
1655     /*
1656     * subtitle of -1 is a scan
1657     */
1658     if( job->subtitle == -1 )
1659     {
1660         char *x264opts_tmp;
1661
1662         /*
1663         * When subtitle scan is enabled do a fast pre-scan job
1664         * which will determine which subtitles to enable, if any.
1665         */
1666         job->pass = -1;
1667         x264opts_tmp = job->x264opts;
1668         job->subtitle = -1;
1669
1670         job->x264opts = NULL;
1671
1672         job->indepth_scan = 1;  
1673
1674         job->select_subtitle = (hb_subtitle_t**)malloc(sizeof(hb_subtitle_t*));
1675         *(job->select_subtitle) = NULL;
1676
1677         /*
1678         * Add the pre-scan job
1679         */
1680         job->sequence_id++; // for job grouping
1681         hb_add( fHandle, job );
1682
1683         job->x264opts = x264opts_tmp;
1684     }
1685     else
1686         job->select_subtitle = NULL;
1687
1688     /* No subtitle were selected, so reset the subtitle to -1 (which before
1689     * this point meant we were scanning
1690     */
1691     if( job->subtitle == -2 )
1692         job->subtitle = -1;
1693
1694     if( [fVidTwoPassCheck state] == NSOnState )
1695     {
1696         hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1697         job->indepth_scan = 0;
1698
1699         /*
1700          * Do not autoselect subtitles on the first pass of a two pass
1701          */
1702         job->select_subtitle = NULL;
1703         
1704         job->pass = 1;
1705         job->sequence_id++; // for job grouping
1706         hb_add( fHandle, job );
1707
1708         job->pass = 2;
1709         job->sequence_id++; // for job grouping
1710
1711         job->x264opts = (char *)calloc(1024, 1); /* Fixme, this just leaks */  
1712         strcpy(job->x264opts, [[fAdvancedOptions optionsString] UTF8String]);
1713
1714         job->select_subtitle = subtitle_tmp;
1715
1716         hb_add( fHandle, job );
1717     }
1718     else
1719     {
1720         job->indepth_scan = 0;
1721         job->pass = 0;
1722         job->sequence_id++; // for job grouping
1723         hb_add( fHandle, job );
1724     }
1725         
1726     NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1727         [[NSUserDefaults standardUserDefaults] setObject:destinationDirectory forKey:@"LastDestinationDirectory"];
1728         
1729     // Notify the queue
1730         [fQueueController hblibJobListChanged];
1731 }
1732
1733 /* Rip: puts up an alert before ultimately calling doRip
1734 */
1735 - (IBAction) Rip: (id) sender
1736 {
1737     /* Rip or Cancel ? */
1738     hb_state_t s;
1739     hb_get_state2( fHandle, &s );
1740
1741     if(s.state == HB_STATE_WORKING || s.state == HB_STATE_PAUSED)
1742         {
1743         [self Cancel: sender];
1744         return;
1745     }
1746     
1747     // If there are jobs in the queue, then this is a rip the queue
1748     
1749     if (hb_count( fHandle ) > 0)
1750     {
1751         [self doRip];
1752         return;
1753     }
1754
1755     // Before adding jobs to the queue, check for a valid destination.
1756
1757     NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1758     if ([[NSFileManager defaultManager] fileExistsAtPath:destinationDirectory] == 0) 
1759     {
1760         NSRunAlertPanel(@"Warning!", @"This is not a valid destination directory!", @"OK", nil, nil);
1761         return;
1762     }
1763
1764     /* We check for duplicate name here */
1765     if( [[NSFileManager defaultManager] fileExistsAtPath:[fDstFile2Field stringValue]] )
1766     {
1767         NSBeginCriticalAlertSheet( _( @"File already exists" ),
1768             _( @"Cancel" ), _( @"Overwrite" ), NULL, fWindow, self,
1769             @selector( overWriteAlertDone:returnCode:contextInfo: ),
1770             NULL, NULL, [NSString stringWithFormat:
1771             _( @"Do you want to overwrite %@?" ),
1772             [fDstFile2Field stringValue]] );
1773             
1774         // overWriteAlertDone: will be called when the alert is dismissed. It will call doRip.
1775     }
1776     else
1777     {
1778         /* if there are no jobs in the queue, then add this one to the queue and rip 
1779         otherwise, just rip the queue */
1780         if( hb_count( fHandle ) == 0)
1781         {
1782             [self doAddToQueue];
1783         }
1784
1785         NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1786         [[NSUserDefaults standardUserDefaults] setObject:destinationDirectory forKey:@"LastDestinationDirectory"];
1787         [self doRip];
1788     }
1789 }
1790
1791 /* overWriteAlertDone: called from the alert posted by Rip: that asks the user if they
1792    want to overwrite an exiting movie file.
1793 */
1794 - (void) overWriteAlertDone: (NSWindow *) sheet
1795     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1796 {
1797     if( returnCode == NSAlertAlternateReturn )
1798     {
1799         /* if there are no jobs in the queue, then add this one to the queue and rip 
1800         otherwise, just rip the queue */
1801         if( hb_count( fHandle ) == 0 )
1802         {
1803             [self doAddToQueue];
1804         }
1805
1806         NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1807         [[NSUserDefaults standardUserDefaults] setObject:destinationDirectory forKey:@"LastDestinationDirectory"];
1808         [self doRip];
1809     }
1810 }
1811
1812 - (void) remindUserOfSleepOrShutdown
1813 {
1814        if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Put Computer To Sleep"])
1815        {
1816                /*Warn that computer will sleep after encoding*/
1817                int reminduser;
1818                NSBeep();
1819                reminduser = NSRunAlertPanel(@"The computer will sleep after encoding is done.",@"You have selected to sleep the computer after encoding. To turn off sleeping, go to the HandBrake preferences.", @"OK", @"Preferences...", nil);
1820                [NSApp requestUserAttention:NSCriticalRequest];
1821                if ( reminduser == NSAlertAlternateReturn ) 
1822                {
1823                        [self showPreferencesWindow:NULL];
1824                }
1825        } 
1826        else if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Shut Down Computer"])
1827        {
1828                /*Warn that computer will shut down after encoding*/
1829                int reminduser;
1830                NSBeep();
1831                reminduser = NSRunAlertPanel(@"The computer will shut down after encoding is done.",@"You have selected to shut down the computer after encoding. To turn off shut down, go to the HandBrake preferences.", @"OK", @"Preferences...", nil);
1832                [NSApp requestUserAttention:NSCriticalRequest];
1833                if ( reminduser == NSAlertAlternateReturn ) 
1834                {
1835                        [self showPreferencesWindow:NULL];
1836                }
1837        }
1838
1839 }
1840
1841
1842 - (void) doRip
1843 {
1844     /* Let libhb do the job */
1845     hb_start( fHandle );
1846         /*set the fEncodeState State */
1847         fEncodeState = 1;
1848 }
1849
1850
1851
1852
1853 //------------------------------------------------------------------------------------
1854 // Removes all jobs from the queue. Does not cancel the current processing job.
1855 //------------------------------------------------------------------------------------
1856 - (void) doDeleteQueuedJobs
1857 {
1858     hb_job_t * job;
1859     while( ( job = hb_job( fHandle, 0 ) ) )
1860         hb_rem( fHandle, job );
1861 }
1862
1863 //------------------------------------------------------------------------------------
1864 // Cancels the current job and proceeds with the next one in the queue.
1865 //------------------------------------------------------------------------------------
1866 - (void) doCancelCurrentJob
1867 {
1868     // Stop the current job. hb_stop will only cancel the current pass and then set
1869     // its state to HB_STATE_WORKDONE. It also does this asynchronously. So when we
1870     // see the state has changed to HB_STATE_WORKDONE (in updateUI), we'll delete the
1871     // remaining passes of the job and then start the queue back up if there are any
1872     // remaining jobs.
1873      
1874     [fQueueController hblibWillStop];
1875     hb_stop( fHandle );
1876     fEncodeState = 2;   // don't alert at end of processing since this was a cancel
1877     
1878 }
1879
1880 //------------------------------------------------------------------------------------
1881 // Displays an alert asking user if the want to cancel encoding of current job.
1882 // Cancel: returns immediately after posting the alert. Later, when the user
1883 // acknowledges the alert, doCancelCurrentJob is called.
1884 //------------------------------------------------------------------------------------
1885 - (IBAction)Cancel: (id)sender
1886 {
1887     if (!fHandle) return;
1888     
1889     hb_job_t * job = hb_current_job(fHandle);
1890     if (!job) return;
1891
1892     NSString * alertTitle = [NSString stringWithFormat:NSLocalizedString(@"Do you want to stop encoding of %@?", nil),
1893             [NSString stringWithUTF8String:job->title->name]];
1894     
1895     // Which window to attach the sheet to?
1896     NSWindow * docWindow;
1897     if ([sender respondsToSelector: @selector(window)])
1898         docWindow = [sender window];
1899     else
1900         docWindow = fWindow;
1901         
1902     NSBeginCriticalAlertSheet(
1903             alertTitle,
1904             NSLocalizedString(@"Keep Encoding", nil),
1905             NSLocalizedString(@"Delete All", nil),
1906             NSLocalizedString(@"Stop Encoding", nil),
1907             docWindow, self,
1908             nil, @selector(didDimissCancelCurrentJob:returnCode:contextInfo:), nil,
1909             NSLocalizedString(@"Your movie will be lost if you don't continue encoding.", nil),
1910             [NSString stringWithUTF8String:job->title->name]);
1911     
1912     // didDimissCancelCurrentJob:returnCode:contextInfo: will be called when the dialog is dismissed
1913
1914     // N.B.: didDimissCancelCurrentJob:returnCode:contextInfo: is designated as the dismiss
1915     // selector to prevent a crash. As a dismiss selector, the alert window will
1916     // have already be dismissed. If we don't do it this way, the dismissing of
1917     // the alert window will cause the table view to be redrawn at a point where
1918     // current job has been deleted by hblib but we don't know about it yet. This
1919     // is a prime example of wy we need to NOT be relying on hb_current_job!!!!
1920 }
1921
1922 - (void) didDimissCancelCurrentJob: (NSWindow *)sheet returnCode: (int)returnCode contextInfo: (void *)contextInfo
1923 {
1924     if (returnCode == NSAlertOtherReturn)
1925         [self doCancelCurrentJob];
1926     else if (returnCode == NSAlertAlternateReturn)
1927     {
1928         [self doDeleteQueuedJobs];
1929         [self doCancelCurrentJob];
1930     }
1931 }
1932
1933
1934
1935
1936
1937 - (IBAction) Pause: (id) sender
1938 {
1939     hb_state_t s;
1940     hb_get_state2( fHandle, &s );
1941
1942     if( s.state == HB_STATE_PAUSED )
1943     {
1944         hb_resume( fHandle );
1945     }
1946     else
1947     {
1948         hb_pause( fHandle );
1949     }
1950 }
1951
1952 #pragma mark -
1953 #pragma mark GUI Controls Changed Methods
1954
1955 - (IBAction) titlePopUpChanged: (id) sender
1956 {
1957     hb_list_t  * list  = hb_get_titles( fHandle );
1958     hb_title_t * title = (hb_title_t*)
1959         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
1960                 
1961                 
1962     /* If Auto Naming is on. We create an output filename of dvd name - title number */
1963     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultAutoNaming"] > 0)
1964         {
1965                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1966                         @"%@/%@-%d.%@", [[fDstFile2Field stringValue] stringByDeletingLastPathComponent],
1967                         [NSString stringWithUTF8String: title->name],
1968                           title->index,
1969                         [[fDstFile2Field stringValue] pathExtension]]]; 
1970         }
1971
1972     /* Update chapter popups */
1973     [fSrcChapterStartPopUp removeAllItems];
1974     [fSrcChapterEndPopUp   removeAllItems];
1975     for( int i = 0; i < hb_list_count( title->list_chapter ); i++ )
1976     {
1977         [fSrcChapterStartPopUp addItemWithTitle: [NSString
1978             stringWithFormat: @"%d", i + 1]];
1979         [fSrcChapterEndPopUp addItemWithTitle: [NSString
1980             stringWithFormat: @"%d", i + 1]];
1981     }
1982     [fSrcChapterStartPopUp selectItemAtIndex: 0];
1983     [fSrcChapterEndPopUp   selectItemAtIndex:
1984         hb_list_count( title->list_chapter ) - 1];
1985     [self chapterPopUpChanged: NULL];
1986
1987 /* Start Get and set the initial pic size for display */
1988         hb_job_t * job = title->job;
1989         fTitle = title; 
1990         /* Turn Deinterlace on/off depending on the preference */
1991         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultDeinterlaceOn"] > 0)
1992         {
1993                 [fPictureController setDeinterlace:1];
1994         }
1995         else
1996         {
1997                 [fPictureController setDeinterlace:0];
1998         }
1999         
2000         /* Pixel Ratio Setting */
2001         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"PixelRatio"])
2002     {
2003                 job->pixel_ratio = 1 ;
2004         }
2005         else
2006         {
2007                 job->pixel_ratio = 0 ;
2008         }
2009         /*Set Source Size Fields Here */
2010         [fPicSrcWidth setStringValue: [NSString stringWithFormat:
2011                                                          @"%d", fTitle->width]];
2012         [fPicSrcHeight setStringValue: [NSString stringWithFormat:
2013                                                          @"%d", fTitle->height]];
2014                                                          
2015         /* Set Auto Crop to on upon selecting a new title */
2016     [fPictureController setAutoCrop:YES];
2017     
2018         /* We get the originial output picture width and height and put them
2019         in variables for use with some presets later on */
2020         PicOrigOutputWidth = job->width;
2021         PicOrigOutputHeight = job->height;
2022         AutoCropTop = job->crop[0];
2023         AutoCropBottom = job->crop[1];
2024         AutoCropLeft = job->crop[2];
2025         AutoCropRight = job->crop[3];
2026         /* we test getting the max output value for pic sizing here to be used later*/
2027         [fPicSettingWidth setStringValue: [NSString stringWithFormat:
2028                 @"%d", PicOrigOutputWidth]];
2029         [fPicSettingHeight setStringValue: [NSString stringWithFormat:
2030                 @"%d", PicOrigOutputHeight]];
2031         /* we run the picture size values through
2032         calculatePictureSizing to get all picture size
2033         information*/
2034         [self calculatePictureSizing: NULL];
2035         /* Run Through encoderPopUpChanged to see if there
2036                 needs to be any pic value modifications based on encoder settings */
2037         //[self encoderPopUpChanged: NULL];
2038         /* END Get and set the initial pic size for display */ 
2039
2040     /* Update subtitle popups */
2041     hb_subtitle_t * subtitle;
2042     [fSubPopUp removeAllItems];
2043     [fSubPopUp addItemWithTitle: @"None"];
2044     [fSubPopUp addItemWithTitle: @"Autoselect"];
2045     for( int i = 0; i < hb_list_count( title->list_subtitle ); i++ )
2046     {
2047         subtitle = (hb_subtitle_t *) hb_list_item( title->list_subtitle, i );
2048
2049         /* We cannot use NSPopUpButton's addItemWithTitle because
2050            it checks for duplicate entries */
2051         [[fSubPopUp menu] addItemWithTitle: [NSString stringWithCString:
2052             subtitle->lang] action: NULL keyEquivalent: @""];
2053     }
2054     [fSubPopUp selectItemAtIndex: 0];
2055         
2056         [self subtitleSelectionChanged: NULL];
2057     
2058     /* Update chapter table */
2059     [fChapterTitlesDelegate resetWithTitle:title];
2060     [fChapterTable reloadData];
2061
2062     /* Update audio popups */
2063     [self addAllAudioTracksToPopUp: fAudLang1PopUp];
2064     [self addAllAudioTracksToPopUp: fAudLang2PopUp];
2065     /* search for the first instance of our prefs default language for track 1, and set track 2 to "none" */
2066         NSString * audioSearchPrefix = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultLanguage"];
2067         [self selectAudioTrackInPopUp: fAudLang1PopUp searchPrefixString: audioSearchPrefix selectIndexIfNotFound: 1];
2068     [self selectAudioTrackInPopUp: fAudLang2PopUp searchPrefixString: NULL selectIndexIfNotFound: 0];
2069         
2070         /* changing the title may have changed the audio channels on offer, */
2071         /* so call audioTrackPopUpChanged for both audio tracks to update the mixdown popups */
2072         [self audioTrackPopUpChanged: fAudLang1PopUp];
2073         [self audioTrackPopUpChanged: fAudLang2PopUp];
2074     
2075     /* We repopulate the Video Framerate popup and show the detected framerate along with "Same as Source"*/
2076     [fVidRatePopUp removeAllItems];
2077     if (fTitle->rate_base == 1126125) // 23.976 NTSC Film
2078     {
2079         [fVidRatePopUp addItemWithTitle: @"Same as source (23.976)"];
2080     }
2081     else if (fTitle->rate_base == 1080000) // 25 PAL Film/Video
2082     {
2083         [fVidRatePopUp addItemWithTitle: @"Same as source (25)"];
2084     }
2085     else if (fTitle->rate_base == 900900) // 29.97 NTSC Video
2086     {
2087         [fVidRatePopUp addItemWithTitle: @"Same as source (29.97)"];
2088     }
2089     else
2090     {
2091         /* if none of the common dvd source framerates is detected, just use "Same as source" */
2092         [fVidRatePopUp addItemWithTitle: @"Same as source"];
2093     }
2094         for( int i = 0; i < hb_video_rates_count; i++ )
2095     {
2096         if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%.3f",23.976]])
2097                 {
2098                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
2099                                 [NSString stringWithCString: hb_video_rates[i].string], @" (NTSC Film)"]];
2100                 }
2101                 else if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%d",25]])
2102                 {
2103                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
2104                                 [NSString stringWithCString: hb_video_rates[i].string], @" (PAL Film/Video)"]];
2105                 }
2106                 else if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%.2f",29.97]])
2107                 {
2108                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
2109                                 [NSString stringWithCString: hb_video_rates[i].string], @" (NTSC Video)"]];
2110                 }
2111                 else
2112                 {
2113                         [fVidRatePopUp addItemWithTitle:
2114                                 [NSString stringWithCString: hb_video_rates[i].string]];
2115                 }
2116     }   
2117     [fVidRatePopUp selectItemAtIndex: 0];
2118     
2119    /* lets call tableViewSelected to make sure that any preset we have selected is enforced after a title change */
2120         [self tableViewSelected:NULL]; 
2121         
2122 }
2123
2124 - (IBAction) chapterPopUpChanged: (id) sender
2125 {
2126     
2127         /* If start chapter popup is greater than end chapter popup,
2128         we set the end chapter popup to the same as start chapter popup */
2129         if ([fSrcChapterStartPopUp indexOfSelectedItem] > [fSrcChapterEndPopUp indexOfSelectedItem])
2130         {
2131                 [fSrcChapterEndPopUp selectItemAtIndex: [fSrcChapterStartPopUp indexOfSelectedItem]];
2132     }
2133
2134                 
2135         hb_list_t  * list  = hb_get_titles( fHandle );
2136     hb_title_t * title = (hb_title_t *)
2137         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
2138
2139     hb_chapter_t * chapter;
2140     int64_t        duration = 0;
2141     for( int i = [fSrcChapterStartPopUp indexOfSelectedItem];
2142          i <= [fSrcChapterEndPopUp indexOfSelectedItem]; i++ )
2143     {
2144         chapter = (hb_chapter_t *) hb_list_item( title->list_chapter, i );
2145         duration += chapter->duration;
2146     }
2147     
2148     duration /= 90000; /* pts -> seconds */
2149     [fSrcDuration2Field setStringValue: [NSString stringWithFormat:
2150         @"%02lld:%02lld:%02lld", duration / 3600, ( duration / 60 ) % 60,
2151         duration % 60]];
2152
2153     [self calculateBitrate: sender];
2154 }
2155
2156 - (IBAction) formatPopUpChanged: (id) sender
2157 {
2158     NSString * string = [fDstFile2Field stringValue];
2159     NSString * selectedCodecs = [fDstCodecsPopUp titleOfSelectedItem];
2160     int format = [fDstFormatPopUp indexOfSelectedItem];
2161     char * ext = NULL;
2162         /* Initially set the large file (64 bit formatting) output checkbox to hidden */
2163     [fDstMpgLargeFileCheck setHidden: YES];
2164     /* Update the codecs popup */
2165     [fDstCodecsPopUp removeAllItems];
2166     switch( format )
2167     {
2168         case 0:
2169                         /*Get Default MP4 File Extension*/
2170                         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultMpegName"] > 0)
2171                         {
2172                                 ext = "m4v";
2173                         }
2174                         else
2175                         {
2176                                 ext = "mp4";
2177                         }
2178             
2179             [fDstCodecsPopUp addItemWithTitle:_( @"MPEG-4 Video / AAC Audio" )];
2180             [fDstCodecsPopUp addItemWithTitle:_( @"AVC/H.264 Video / AAC Audio" )];
2181             
2182                         /* We enable the create chapters checkbox here since we are .mp4*/
2183                         [fCreateChapterMarkers setEnabled: YES];
2184                         /* We show the Large File (64 bit formatting) checkbox since we are .mp4 
2185                         if we have enabled the option in the global preferences*/
2186                         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AllowLargeFiles"] > 0)
2187                         {
2188                                 [fDstMpgLargeFileCheck setHidden: NO];
2189                         }
2190                                 else
2191                                 {
2192                                         /* if not enable in global preferences, we additionaly sanity check that the
2193                                         hidden checkbox is set to off. */
2194                     [fDstMpgLargeFileCheck setState: NSOffState];
2195                                 }
2196             break;
2197             
2198         case 1:
2199             ext = "mkv";
2200             [fDstCodecsPopUp addItemWithTitle:_( @"MPEG-4 Video / AAC Audio" )];
2201             [fDstCodecsPopUp addItemWithTitle:_( @"MPEG-4 Video / AC-3 Audio" )];
2202                         [fDstCodecsPopUp addItemWithTitle:_( @"MPEG-4 Video / MP3 Audio" )];
2203                         [fDstCodecsPopUp addItemWithTitle:_( @"MPEG-4 Video / Vorbis Audio" )];
2204             
2205                         [fDstCodecsPopUp addItemWithTitle:_( @"AVC/H.264 Video / AAC Audio" )];
2206                         [fDstCodecsPopUp addItemWithTitle:_( @"AVC/H.264 Video / AC-3 Audio" )];
2207                         [fDstCodecsPopUp addItemWithTitle:_( @"AVC/H.264 Video / MP3 Audio" )];
2208                         [fDstCodecsPopUp addItemWithTitle:_( @"AVC/H.264 Video / Vorbis Audio" )];
2209             /* We enable the create chapters checkbox here */
2210                         [fCreateChapterMarkers setEnabled: YES];
2211                         break;
2212             
2213         case 2: 
2214             ext = "avi";
2215             [fDstCodecsPopUp addItemWithTitle:_( @"MPEG-4 Video / MP3 Audio" )];
2216             [fDstCodecsPopUp addItemWithTitle:_( @"MPEG-4 Video / AC-3 Audio" )];
2217             [fDstCodecsPopUp addItemWithTitle:_( @"AVC/H.264 Video / MP3 Audio" )];
2218             [fDstCodecsPopUp addItemWithTitle:_( @"AVC/H.264 Video / AC-3 Audio" )];
2219                         /* We disable the create chapters checkbox here and make sure it is unchecked*/
2220                         [fCreateChapterMarkers setEnabled: NO];
2221                         [fCreateChapterMarkers setState: NSOffState];
2222                         break;
2223             
2224         case 3:
2225             ext = "ogm";
2226             [fDstCodecsPopUp addItemWithTitle:_( @"MPEG-4 Video / Vorbis Audio" )];
2227             [fDstCodecsPopUp addItemWithTitle:_( @"MPEG-4 Video / MP3 Audio" )];
2228             /* We disable the create chapters checkbox here and make sure it is unchecked*/
2229                         [fCreateChapterMarkers setEnabled: NO];
2230                         [fCreateChapterMarkers setState: NSOffState];
2231                         break;
2232     }
2233     
2234     if ( SuccessfulScan ) {
2235         [fDstCodecsPopUp selectItemWithTitle:selectedCodecs];
2236         
2237         /* Add/replace to the correct extension */
2238         if( [string characterAtIndex: [string length] - 4] == '.' )
2239         {
2240             [fDstFile2Field setStringValue: [NSString stringWithFormat:
2241                 @"%@.%s", [string substringToIndex: [string length] - 4],
2242                 ext]];
2243         }
2244         else
2245         {
2246             [fDstFile2Field setStringValue: [NSString stringWithFormat:
2247                 @"%@.%s", string, ext]];
2248         }
2249         
2250         if ( [fDstCodecsPopUp selectedItem] == NULL )
2251         {
2252             [fDstCodecsPopUp selectItemAtIndex:0];
2253             [self codecsPopUpChanged: NULL];
2254             
2255             /* changing the format may mean that we can / can't offer mono or 6ch, */
2256             /* so call audioTrackPopUpChanged for both audio tracks to update the mixdown popups */
2257             [self audioTrackPopUpChanged: fAudLang1PopUp];
2258             [self audioTrackPopUpChanged: fAudLang2PopUp];
2259             /* We call the method to properly enable/disable turbo 2 pass */
2260             [self twoPassCheckboxChanged: sender];
2261             /* We call method method to change UI to reflect whether a preset is used or not*/
2262         }
2263     }
2264     
2265         [self customSettingUsed: sender];       
2266 }
2267
2268 - (IBAction) codecsPopUpChanged: (id) sender
2269 {
2270     int format = [fDstFormatPopUp indexOfSelectedItem];
2271     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2272         
2273     [fAdvancedOptions setHidden:YES];
2274
2275     /* Update the encoder popup*/
2276     if( ( FormatSettings[format][codecs] & HB_VCODEC_X264 ) )
2277     {
2278         /* MPEG-4 -> H.264 */
2279         [fVidEncoderPopUp removeAllItems];
2280                 [fVidEncoderPopUp addItemWithTitle: @"x264 (h.264 Main)"];
2281                 [fVidEncoderPopUp addItemWithTitle: @"x264 (h.264 iPod)"];
2282                 [fVidEncoderPopUp selectItemAtIndex: 0];
2283         [fAdvancedOptions setHidden:NO];
2284     }
2285     
2286     else if( ( FormatSettings[format][codecs] & HB_VCODEC_FFMPEG ) )
2287     {
2288         /* H.264 -> MPEG-4 */
2289         [fVidEncoderPopUp removeAllItems];
2290         [fVidEncoderPopUp addItemWithTitle: @"FFmpeg"];
2291         [fVidEncoderPopUp addItemWithTitle: @"XviD"];
2292         [fVidEncoderPopUp selectItemAtIndex: 0];
2293                                 
2294     }
2295
2296     if( FormatSettings[format][codecs] & HB_ACODEC_AC3 )
2297     {
2298         /* AC-3 pass-through: disable samplerate and bitrate */
2299         [fAudRatePopUp    setEnabled: NO];
2300         [fAudBitratePopUp setEnabled: NO];
2301     }
2302     else
2303     {
2304         [fAudRatePopUp    setEnabled: YES];
2305         [fAudBitratePopUp setEnabled: YES];
2306     }
2307     /* changing the codecs on offer may mean that we can / can't offer mono or 6ch, */
2308         /* so call audioTrackPopUpChanged for both audio tracks to update the mixdown popups */
2309         [self audioTrackPopUpChanged: fAudLang1PopUp];
2310         [self audioTrackPopUpChanged: fAudLang2PopUp];
2311
2312     [self calculateBitrate: sender];
2313     [self twoPassCheckboxChanged: sender];
2314 }
2315
2316 - (IBAction) encoderPopUpChanged: (id) sender
2317 {
2318     
2319         /* Check to see if we need to modify the job pic values based on x264 (iPod) encoder selection */
2320     if ([fDstFormatPopUp indexOfSelectedItem] == 0 && [fDstCodecsPopUp indexOfSelectedItem] == 1 && [fVidEncoderPopUp indexOfSelectedItem] == 1)
2321     {
2322                 hb_job_t * job = fTitle->job;
2323                 job->pixel_ratio = 0 ;
2324                 
2325                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultPicSizeAutoiPod"] > 0)
2326                 {
2327                         
2328                         if (job->width > 640)
2329                         {
2330                                 job->width = 640;
2331                         }
2332                         job->keep_ratio = 1;
2333                         hb_fix_aspect( job, HB_KEEP_WIDTH );
2334                         
2335                 }
2336                 /* Make sure the 64bit formatting checkbox is off */
2337                 [fDstMpgLargeFileCheck setState: NSOffState];
2338         }
2339     
2340         [self calculatePictureSizing: sender];
2341         [self twoPassCheckboxChanged: sender];
2342 }
2343
2344 /* Method to determine if we should change the UI
2345 To reflect whether or not a Preset is being used or if
2346 the user is using "Custom" settings by determining the sender*/
2347 - (IBAction) customSettingUsed: (id) sender
2348 {
2349         if ([sender stringValue] != NULL)
2350         {
2351                 /* Deselect the currently selected Preset if there is one*/
2352                 [tableView deselectRow:[tableView selectedRow]];
2353                 [[fPresetsActionMenu itemAtIndex:0] setEnabled: NO];
2354                 /* Change UI to show "Custom" settings are being used */
2355                 [fPresetSelectedDisplay setStringValue: @"Custom"];
2356                 
2357                 curUserPresetChosenNum = nil;
2358         }
2359
2360 }
2361
2362
2363 #pragma mark -
2364 #pragma mark - Video
2365
2366 - (IBAction) twoPassCheckboxChanged: (id) sender
2367 {
2368         /* check to see if x264 is chosen */
2369         int format = [fDstFormatPopUp indexOfSelectedItem];
2370     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2371         if( ( FormatSettings[format][codecs] & HB_VCODEC_X264 ) )
2372     {
2373                 if( [fVidTwoPassCheck state] == NSOnState)
2374                 {
2375                         [fVidTurboPassCheck setHidden: NO];
2376                 }
2377                 else
2378                 {
2379                         [fVidTurboPassCheck setHidden: YES];
2380                         [fVidTurboPassCheck setState: NSOffState];
2381                 }
2382                 /* Make sure Two Pass is checked if Turbo is checked */
2383                 if( [fVidTurboPassCheck state] == NSOnState)
2384                 {
2385                         [fVidTwoPassCheck setState: NSOnState];
2386                 }
2387         }
2388         else
2389         {
2390                 [fVidTurboPassCheck setHidden: YES];
2391                 [fVidTurboPassCheck setState: NSOffState];
2392         }
2393         
2394         /* We call method method to change UI to reflect whether a preset is used or not*/
2395         [self customSettingUsed: sender];
2396 }
2397
2398 - (IBAction ) videoFrameRateChanged: (id) sender
2399 {
2400     /* We call method method to calculatePictureSizing to error check detelecine*/
2401     [self calculatePictureSizing: sender];
2402
2403     /* We call method method to change UI to reflect whether a preset is used or not*/
2404         [self customSettingUsed: sender];
2405 }
2406 - (IBAction) videoMatrixChanged: (id) sender;
2407 {
2408     bool target, bitrate, quality;
2409
2410     target = bitrate = quality = false;
2411     if( [fVidQualityMatrix isEnabled] )
2412     {
2413         switch( [fVidQualityMatrix selectedRow] )
2414         {
2415             case 0:
2416                 target = true;
2417                 break;
2418             case 1:
2419                 bitrate = true;
2420                 break;
2421             case 2:
2422                 quality = true;
2423                 break;
2424         }
2425     }
2426     [fVidTargetSizeField  setEnabled: target];
2427     [fVidBitrateField     setEnabled: bitrate];
2428     [fVidQualitySlider    setEnabled: quality];
2429     [fVidTwoPassCheck     setEnabled: !quality &&
2430         [fVidQualityMatrix isEnabled]];
2431     if( quality )
2432     {
2433         [fVidTwoPassCheck setState: NSOffState];
2434                 [fVidTurboPassCheck setHidden: YES];
2435                 [fVidTurboPassCheck setState: NSOffState];
2436     }
2437
2438     [self qualitySliderChanged: sender];
2439     [self calculateBitrate: sender];
2440         [self customSettingUsed: sender];
2441 }
2442
2443 - (IBAction) qualitySliderChanged: (id) sender
2444 {
2445     [fVidConstantCell setTitle: [NSString stringWithFormat:
2446         _( @"Constant quality: %.0f %%" ), 100.0 *
2447         [fVidQualitySlider floatValue]]];
2448                 [self customSettingUsed: sender];
2449 }
2450
2451 - (void) controlTextDidChange: (NSNotification *) notification
2452 {
2453     [self calculateBitrate: NULL];
2454 }
2455
2456 - (IBAction) calculateBitrate: (id) sender
2457 {
2458     if( !fHandle || [fVidQualityMatrix selectedRow] != 0 || !SuccessfulScan )
2459     {
2460         return;
2461     }
2462
2463     hb_list_t  * list  = hb_get_titles( fHandle );
2464     hb_title_t * title = (hb_title_t *) hb_list_item( list,
2465             [fSrcTitlePopUp indexOfSelectedItem] );
2466     hb_job_t * job = title->job;
2467
2468     [self prepareJob];
2469
2470     [fVidBitrateField setIntValue: hb_calc_bitrate( job,
2471             [fVidTargetSizeField intValue] )];
2472 }
2473
2474 #pragma mark -
2475 #pragma mark - Picture
2476
2477 /* lets set the picture size back to the max from right after title scan
2478    Lets use an IBAction here as down the road we could always use a checkbox
2479    in the gui to easily take the user back to max. Remember, the compiler
2480    resolves IBActions down to -(void) during compile anyway */
2481 - (IBAction) revertPictureSizeToMax: (id) sender
2482 {
2483         hb_job_t * job = fTitle->job;
2484         /* We use the output picture width and height
2485      as calculated from libhb right after title is set
2486      in TitlePopUpChanged */
2487         job->width = PicOrigOutputWidth;
2488         job->height = PicOrigOutputHeight;
2489     [fPictureController setAutoCrop:YES];
2490         /* Here we use the auto crop values determined right after scan */
2491         job->crop[0] = AutoCropTop;
2492         job->crop[1] = AutoCropBottom;
2493         job->crop[2] = AutoCropLeft;
2494         job->crop[3] = AutoCropRight;
2495     
2496     
2497     [self calculatePictureSizing: sender];
2498     /* We call method to change UI to reflect whether a preset is used or not*/    
2499     [self customSettingUsed: sender];
2500 }
2501
2502 /**
2503  * Registers changes made in the Picture Settings Window.
2504  */
2505
2506 - (void)pictureSettingsDidChange {
2507         [self calculatePictureSizing: NULL];
2508 }
2509
2510 /* Get and Display Current Pic Settings in main window */
2511 - (IBAction) calculatePictureSizing: (id) sender
2512 {
2513         [fPicSettingWidth setStringValue: [NSString stringWithFormat:@"%d", fTitle->job->width]];
2514         [fPicSettingHeight setStringValue: [NSString stringWithFormat:@"%d", fTitle->job->height]];
2515                 
2516         if (fTitle->job->pixel_ratio == 1)
2517         {
2518         int titlewidth = fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3];
2519         int arpwidth = fTitle->job->pixel_aspect_width;
2520         int arpheight = fTitle->job->pixel_aspect_height;
2521         int displayparwidth = titlewidth * arpwidth / arpheight;
2522         int displayparheight = fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1];
2523         
2524         [fPicSettingWidth setStringValue: [NSString stringWithFormat:@"%d", titlewidth]];
2525         [fPicSettingHeight setStringValue: [NSString stringWithFormat:@"%d", displayparheight]];
2526         [fPicLabelPAROutputX setStringValue: @"x"];
2527         [fPicSettingPARWidth setStringValue: [NSString stringWithFormat:@"%d", displayparwidth]];
2528         [fPicSettingPARHeight setStringValue: [NSString stringWithFormat:@"%d", displayparheight]];
2529         
2530         fTitle->job->keep_ratio = 0;
2531         }
2532         else
2533         {
2534         [fPicLabelPAROutputX setStringValue: @""];
2535         [fPicSettingPARWidth setStringValue: @""];
2536         [fPicSettingPARHeight setStringValue:  @""];
2537         }
2538                                 
2539         /* Set ON/Off values for the deinterlace/keep aspect ratio according to boolean */      
2540         if (fTitle->job->keep_ratio > 0)
2541         {
2542                 [fPicSettingARkeep setStringValue: @"On"];
2543         }
2544         else
2545         {
2546                 [fPicSettingARkeep setStringValue: @"Off"];
2547         }       
2548     /* Detelecine */
2549     if ([fPictureController detelecine]) {
2550         [fPicSettingDetelecine setStringValue: @"Yes"];
2551     }
2552     else {
2553         [fPicSettingDetelecine setStringValue: @"No"];
2554     }
2555     
2556     /* VFR (Variable Frame Rate) */
2557     if ([fPictureController vfr]) {
2558         /* vfr has to set the framerate to 29.97 (ntsc video)
2559         and disable the framerate popup */
2560         [fVidRatePopUp selectItemAtIndex: 8];
2561         [fVidRatePopUp setEnabled: NO];
2562         /* We change the string of the fps popup to warn that vfr is on Framerate (FPS): */
2563         [fVidRateField setStringValue: @"Framerate (VFR On):"];  
2564         
2565     }
2566     else {
2567         /* vfr is off, make sure the framerate popup is enabled */
2568         [fVidRatePopUp setEnabled: YES];
2569         /* and make sure the label for framerate is set to its default */  
2570         [fVidRateField setStringValue: @"Framerate (FPS):"];
2571     }
2572
2573         /* Deinterlace */
2574         if ([fPictureController deinterlace] == 0)
2575         {
2576                 [fPicSettingDeinterlace setStringValue: @"Off"];
2577         }
2578         else if ([fPictureController deinterlace] == 1)
2579         {
2580                 [fPicSettingDeinterlace setStringValue: @"Fast"];
2581         }
2582         else if ([fPictureController deinterlace] == 2)
2583         {
2584                 [fPicSettingDeinterlace setStringValue: @"Slow"];
2585         }
2586         else if ([fPictureController deinterlace] == 3)
2587         {
2588                 [fPicSettingDeinterlace setStringValue: @"Slower"];
2589         }
2590         else if ([fPictureController deinterlace] ==4)
2591         {
2592                 [fPicSettingDeinterlace setStringValue: @"Slowest"];
2593         }
2594         /* Denoise */
2595         if ([fPictureController denoise] == 0)
2596         {
2597                 [fPicSettingDenoise setStringValue: @"Off"];
2598         }
2599         else if ([fPictureController denoise] == 1)
2600         {
2601                 [fPicSettingDenoise setStringValue: @"Weak"];
2602         }
2603         else if ([fPictureController denoise] == 2)
2604         {
2605                 [fPicSettingDenoise setStringValue: @"Medium"];
2606         }
2607         else if ([fPictureController denoise] == 3)
2608         {
2609                 [fPicSettingDenoise setStringValue: @"Strong"];
2610         }
2611
2612     /* Deblock */
2613     if ([fPictureController deblock]) {
2614         [fPicSettingDeblock setStringValue: @"Yes"];
2615     }
2616     else {
2617         [fPicSettingDeblock setStringValue: @"No"];
2618     }
2619         
2620         if (fTitle->job->pixel_ratio > 0)
2621         {
2622                 [fPicSettingPAR setStringValue: @""];
2623         }
2624         else
2625         {
2626                 [fPicSettingPAR setStringValue: @"Off"];
2627         }
2628         /* Set the display field for crop as per boolean */
2629         if (![fPictureController autoCrop])
2630         {
2631             [fPicSettingAutoCrop setStringValue: @"Custom"];
2632         }
2633         else
2634         {
2635                 [fPicSettingAutoCrop setStringValue: @"Auto"];
2636         }       
2637         
2638
2639 }
2640
2641
2642 #pragma mark -
2643 #pragma mark - Audio and Subtitles
2644
2645 - (IBAction) setEnabledStateOfAudioMixdownControls: (id) sender
2646 {
2647
2648     /* enable/disable the mixdown text and popupbutton for audio track 1 */
2649     [fAudTrack1MixPopUp setEnabled: ([fAudLang1PopUp indexOfSelectedItem] == 0) ? NO : YES];
2650     [fAudTrack1MixLabel setTextColor: ([fAudLang1PopUp indexOfSelectedItem] == 0) ?
2651         [NSColor disabledControlTextColor] : [NSColor controlTextColor]];
2652
2653     /* enable/disable the mixdown text and popupbutton for audio track 2 */
2654     [fAudTrack2MixPopUp setEnabled: ([fAudLang2PopUp indexOfSelectedItem] == 0) ? NO : YES];
2655     [fAudTrack2MixLabel setTextColor: ([fAudLang2PopUp indexOfSelectedItem] == 0) ?
2656         [NSColor disabledControlTextColor] : [NSColor controlTextColor]];
2657
2658 }
2659
2660 - (IBAction) addAllAudioTracksToPopUp: (id) sender
2661 {
2662
2663     hb_list_t  * list  = hb_get_titles( fHandle );
2664     hb_title_t * title = (hb_title_t*)
2665         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
2666
2667         hb_audio_t * audio;
2668
2669     [sender removeAllItems];
2670     [sender addItemWithTitle: _( @"None" )];
2671     for( int i = 0; i < hb_list_count( title->list_audio ); i++ )
2672     {
2673         audio = (hb_audio_t *) hb_list_item( title->list_audio, i );
2674         [[sender menu] addItemWithTitle:
2675             [NSString stringWithCString: audio->lang]
2676             action: NULL keyEquivalent: @""];
2677     }
2678     [sender selectItemAtIndex: 0];
2679
2680 }
2681
2682 - (IBAction) selectAudioTrackInPopUp: (id) sender searchPrefixString: (NSString *) searchPrefixString selectIndexIfNotFound: (int) selectIndexIfNotFound
2683 {
2684
2685     /* this method can be used to find a language, or a language-and-source-format combination, by passing in the appropriate string */
2686     /* e.g. to find the first French track, pass in an NSString * of "Francais" */
2687     /* e.g. to find the first English 5.1 AC3 track, pass in an NSString * of "English (AC3) (5.1 ch)" */
2688     /* if no matching track is found, then selectIndexIfNotFound is used to choose which track to select instead */
2689     
2690         if (searchPrefixString != NULL) 
2691         {
2692
2693         for( int i = 0; i < [sender numberOfItems]; i++ )
2694         {
2695             /* Try to find the desired search string */
2696             if ([[[sender itemAtIndex: i] title] hasPrefix:searchPrefixString])
2697             {
2698                 [sender selectItemAtIndex: i];
2699                 return;
2700             }
2701         }
2702         /* couldn't find the string, so select the requested "search string not found" item */
2703         /* index of 0 means select the "none" item */
2704         /* index of 1 means select the first audio track */
2705         [sender selectItemAtIndex: selectIndexIfNotFound];
2706         }
2707     else
2708     {
2709         /* if no search string is provided, then select the selectIndexIfNotFound item */
2710         [sender selectItemAtIndex: selectIndexIfNotFound];
2711     }
2712
2713 }
2714
2715 - (IBAction) audioTrackPopUpChanged: (id) sender
2716 {
2717     /* utility function to call audioTrackPopUpChanged without passing in a mixdown-to-use */
2718     [self audioTrackPopUpChanged: sender mixdownToUse: 0];
2719 }
2720
2721 - (IBAction) audioTrackPopUpChanged: (id) sender mixdownToUse: (int) mixdownToUse
2722 {
2723
2724     /* make sure we have a selected title before continuing */
2725     if (fTitle == NULL) return;
2726
2727     /* find out if audio track 1 or 2 was changed - this is passed to us in the tag of the sender */
2728     /* the sender will have been either fAudLang1PopUp (tag = 0) or fAudLang2PopUp (tag = 1) */
2729     int thisAudio = [sender tag];
2730
2731     /* get the index of the selected audio */
2732     int thisAudioIndex = [sender indexOfSelectedItem] - 1;
2733
2734     /* Handbrake can't currently cope with ripping the same source track twice */
2735     /* So, if this audio is also selected in the other audio track popup, set that popup's selection to "none" */
2736     /* get a reference to the two audio track popups */
2737     NSPopUpButton * thisAudioPopUp  = (thisAudio == 1 ? fAudLang2PopUp : fAudLang1PopUp);
2738     NSPopUpButton * otherAudioPopUp = (thisAudio == 1 ? fAudLang1PopUp : fAudLang2PopUp);
2739     /* if the same track is selected in the other audio popup, then select "none" in that popup */
2740     /* unless, of course, both are selected as "none!" */
2741     if ([thisAudioPopUp indexOfSelectedItem] != 0 && [thisAudioPopUp indexOfSelectedItem] == [otherAudioPopUp indexOfSelectedItem]) {
2742         [otherAudioPopUp selectItemAtIndex: 0];
2743         [self audioTrackPopUpChanged: otherAudioPopUp];
2744     }
2745
2746     /* pointer for the hb_audio_s struct we will use later on */
2747     hb_audio_t * audio;
2748
2749     /* find out what the currently-selected output audio codec is */
2750     int format = [fDstFormatPopUp indexOfSelectedItem];
2751     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2752     int acodec = FormatSettings[format][codecs] & HB_ACODEC_MASK;
2753
2754     /* pointer to this track's mixdown NSPopUpButton */
2755     NSTextField   * mixdownTextField;
2756     NSPopUpButton * mixdownPopUp;
2757
2758     /* find our mixdown NSTextField and NSPopUpButton */
2759     if (thisAudio == 0)
2760     {
2761         mixdownTextField = fAudTrack1MixLabel;
2762         mixdownPopUp = fAudTrack1MixPopUp;
2763     }
2764     else
2765     {
2766         mixdownTextField = fAudTrack2MixLabel;
2767         mixdownPopUp = fAudTrack2MixPopUp;
2768     }
2769
2770     /* delete the previous audio mixdown options */
2771     [mixdownPopUp removeAllItems];
2772
2773     /* check if the audio mixdown controls need their enabled state changing */
2774     [self setEnabledStateOfAudioMixdownControls: NULL];
2775
2776     if (thisAudioIndex != -1)
2777     {
2778
2779         /* get the audio */
2780         audio = (hb_audio_t *) hb_list_item( fTitle->list_audio, thisAudioIndex );
2781         if (audio != NULL)
2782         {
2783
2784             /* find out if our selected output audio codec supports mono and / or 6ch */
2785             /* we also check for an input codec of AC3 or DCA,
2786                as they are the only libraries able to do the mixdown to mono / conversion to 6-ch */
2787             /* audioCodecsSupportMono and audioCodecsSupport6Ch are the same for now,
2788                but this may change in the future, so they are separated for flexibility */
2789             int audioCodecsSupportMono = ((audio->codec == HB_ACODEC_AC3 ||
2790                 audio->codec == HB_ACODEC_DCA) && acodec == HB_ACODEC_FAAC);
2791             int audioCodecsSupport6Ch =  ((audio->codec == HB_ACODEC_AC3 ||
2792                 audio->codec == HB_ACODEC_DCA) && (acodec == HB_ACODEC_FAAC ||
2793                 acodec == HB_ACODEC_VORBIS));
2794
2795             /* check for AC-3 passthru */
2796             if (audio->codec == HB_ACODEC_AC3 && acodec == HB_ACODEC_AC3)
2797             {
2798                     [[mixdownPopUp menu] addItemWithTitle:
2799                         [NSString stringWithCString: "AC3 Passthru"]
2800                         action: NULL keyEquivalent: @""];
2801             }
2802             else
2803             {
2804
2805                 /* add the appropriate audio mixdown menuitems to the popupbutton */
2806                 /* in each case, we set the new menuitem's tag to be the amixdown value for that mixdown,
2807                    so that we can reference the mixdown later */
2808
2809                 /* keep a track of the min and max mixdowns we used, so we can select the best match later */
2810                 int minMixdownUsed = 0;
2811                 int maxMixdownUsed = 0;
2812                 
2813                 /* get the input channel layout without any lfe channels */
2814                 int layout = audio->input_channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
2815
2816                 /* do we want to add a mono option? */
2817                 if (audioCodecsSupportMono == 1) {
2818                     NSMenuItem *menuItem = [[mixdownPopUp menu] addItemWithTitle:
2819                         [NSString stringWithCString: hb_audio_mixdowns[0].human_readable_name]
2820                         action: NULL keyEquivalent: @""];
2821                     [menuItem setTag: hb_audio_mixdowns[0].amixdown];
2822                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[0].amixdown;
2823                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[0].amixdown);
2824                 }
2825
2826                 /* do we want to add a stereo option? */
2827                 /* offer stereo if we have a mono source and non-mono-supporting codecs, as otherwise we won't have a mixdown at all */
2828                 /* also offer stereo if we have a stereo-or-better source */
2829                 if ((layout == HB_INPUT_CH_LAYOUT_MONO && audioCodecsSupportMono == 0) || layout >= HB_INPUT_CH_LAYOUT_STEREO) {
2830                     NSMenuItem *menuItem = [[mixdownPopUp menu] addItemWithTitle:
2831                         [NSString stringWithCString: hb_audio_mixdowns[1].human_readable_name]
2832                         action: NULL keyEquivalent: @""];
2833                     [menuItem setTag: hb_audio_mixdowns[1].amixdown];
2834                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[1].amixdown;
2835                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[1].amixdown);
2836                 }
2837
2838                 /* do we want to add a dolby surround (DPL1) option? */
2839                 if (layout == HB_INPUT_CH_LAYOUT_3F1R || layout == HB_INPUT_CH_LAYOUT_3F2R || layout == HB_INPUT_CH_LAYOUT_DOLBY) {
2840                     NSMenuItem *menuItem = [[mixdownPopUp menu] addItemWithTitle:
2841                         [NSString stringWithCString: hb_audio_mixdowns[2].human_readable_name]
2842                         action: NULL keyEquivalent: @""];
2843                     [menuItem setTag: hb_audio_mixdowns[2].amixdown];
2844                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[2].amixdown;
2845                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[2].amixdown);
2846                 }
2847
2848                 /* do we want to add a dolby pro logic 2 (DPL2) option? */
2849                 if (layout == HB_INPUT_CH_LAYOUT_3F2R) {
2850                     NSMenuItem *menuItem = [[mixdownPopUp menu] addItemWithTitle:
2851                         [NSString stringWithCString: hb_audio_mixdowns[3].human_readable_name]
2852                         action: NULL keyEquivalent: @""];
2853                     [menuItem setTag: hb_audio_mixdowns[3].amixdown];
2854                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[3].amixdown;
2855                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[3].amixdown);
2856                 }
2857
2858                 /* do we want to add a 6-channel discrete option? */
2859                 if (audioCodecsSupport6Ch == 1 && layout == HB_INPUT_CH_LAYOUT_3F2R && (audio->input_channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE)) {
2860                     NSMenuItem *menuItem = [[mixdownPopUp menu] addItemWithTitle:
2861                         [NSString stringWithCString: hb_audio_mixdowns[4].human_readable_name]
2862                         action: NULL keyEquivalent: @""];
2863                     [menuItem setTag: hb_audio_mixdowns[4].amixdown];
2864                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[4].amixdown;
2865                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[4].amixdown);
2866                 }
2867
2868                 /* auto-select the best mixdown based on our saved mixdown preference */
2869                 
2870                 /* for now, this is hard-coded to a "best" mixdown of HB_AMIXDOWN_DOLBYPLII */
2871                 /* ultimately this should be a prefs option */
2872                 int useMixdown;
2873                 
2874                 /* if we passed in a mixdown to use - in order to load a preset - then try and use it */
2875                 if (mixdownToUse > 0)
2876                 {
2877                     useMixdown = mixdownToUse;
2878                 }
2879                 else
2880                 {
2881                     useMixdown = HB_AMIXDOWN_DOLBYPLII;
2882                 }
2883                 
2884                 /* if useMixdown > maxMixdownUsed, then use maxMixdownUsed */
2885                 if (useMixdown > maxMixdownUsed) useMixdown = maxMixdownUsed;
2886
2887                 /* if useMixdown < minMixdownUsed, then use minMixdownUsed */
2888                 if (useMixdown < minMixdownUsed) useMixdown = minMixdownUsed;
2889
2890                 /* select the (possibly-amended) preferred mixdown */
2891                 [mixdownPopUp selectItemWithTag: useMixdown];
2892                                 
2893                                 /* lets call the audioTrackMixdownChanged method here to determine appropriate bitrates, etc. */
2894                 [self audioTrackMixdownChanged: NULL];
2895             }
2896
2897         }
2898         
2899     }
2900
2901         /* see if the new audio track choice will change the bitrate we need */
2902     [self calculateBitrate: sender];    
2903
2904 }
2905 - (IBAction) audioTrackMixdownChanged: (id) sender
2906 {
2907
2908     /* find out what the currently-selected output audio codec is */
2909     int format = [fDstFormatPopUp indexOfSelectedItem];
2910     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2911     int acodec = FormatSettings[format][codecs] & HB_ACODEC_MASK;
2912     
2913     /* storage variable for the min and max bitrate allowed for this codec */
2914     int minbitrate;
2915     int maxbitrate;
2916     
2917     switch( acodec )
2918     {
2919         case HB_ACODEC_FAAC:
2920             /* check if we have a 6ch discrete conversion in either audio track */
2921             if ([[fAudTrack1MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH || [[fAudTrack2MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH)
2922             {
2923                 /* FAAC is happy using our min bitrate of 32 kbps, even for 6ch */
2924                 minbitrate = 32;
2925                 /* If either mixdown popup includes 6-channel discrete, then allow up to 384 kbps */
2926                 maxbitrate = 384;
2927                 break;
2928             }
2929             else
2930             {
2931                 /* FAAC is happy using our min bitrate of 32 kbps for stereo or mono */
2932                 minbitrate = 32;
2933                 /* FAAC won't honour anything more than 160 for stereo, so let's not offer it */
2934                 /* note: haven't dealt with mono separately here, FAAC will just use the max it can */
2935                 maxbitrate = 160;
2936                 break;
2937             }
2938
2939         case HB_ACODEC_LAME:
2940             /* Lame is happy using our min bitrate of 32 kbps */
2941             minbitrate = 32;
2942             /* Lame won't encode if the bitrate is higher than 320 kbps */
2943             maxbitrate = 320;
2944             break;
2945
2946         case HB_ACODEC_VORBIS:
2947         if ([[fAudTrack1MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH || [[fAudTrack2MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH)
2948             {
2949                 /* Vorbis causes a crash if we use a bitrate below 192 kbps with 6 channel */
2950                 minbitrate = 192;
2951                 /* If either mixdown popup includes 6-channel discrete, then allow up to 384 kbps */
2952                 maxbitrate = 384;
2953                 break;
2954             }
2955             else
2956             {
2957             /* Vorbis causes a crash if we use a bitrate below 48 kbps */
2958             minbitrate = 48;
2959             /* Vorbis can cope with 384 kbps quite happily, even for stereo */
2960             maxbitrate = 384;
2961             break;
2962             }
2963
2964         default:
2965             /* AC3 passthru disables the bitrate dropdown anyway, so we might as well just use the min and max bitrate */
2966             minbitrate = 32;
2967             maxbitrate = 384;
2968         
2969     }
2970
2971     [fAudBitratePopUp removeAllItems];
2972
2973     for( int i = 0; i < hb_audio_bitrates_count; i++ )
2974     {
2975         if (hb_audio_bitrates[i].rate >= minbitrate && hb_audio_bitrates[i].rate <= maxbitrate)
2976         {
2977             /* add a new menuitem for this bitrate */
2978             NSMenuItem *menuItem = [[fAudBitratePopUp menu] addItemWithTitle:
2979                 [NSString stringWithCString: hb_audio_bitrates[i].string]
2980                 action: NULL keyEquivalent: @""];
2981             /* set its tag to be the actual bitrate as an integer, so we can retrieve it later */
2982             [menuItem setTag: hb_audio_bitrates[i].rate];
2983         }
2984     }
2985
2986     /* select the default bitrate (but use 384 for 6-ch AAC) */
2987     if ([[fAudTrack1MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH || [[fAudTrack2MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH)
2988     {
2989         [fAudBitratePopUp selectItemWithTag: 384];
2990     }
2991     else
2992     {
2993         [fAudBitratePopUp selectItemWithTag: hb_audio_bitrates[hb_audio_bitrates_default].rate];
2994     }
2995
2996 }
2997
2998 - (IBAction) subtitleSelectionChanged: (id) sender
2999 {
3000         if ([fSubPopUp indexOfSelectedItem] == 0)
3001         {
3002         [fSubForcedCheck setState: NSOffState];
3003         [fSubForcedCheck setEnabled: NO];       
3004         }
3005         else
3006         {
3007         [fSubForcedCheck setEnabled: YES];      
3008         }
3009         
3010 }
3011
3012
3013
3014
3015 #pragma mark -
3016 #pragma mark Open New Windows
3017
3018 - (IBAction) openHomepage: (id) sender
3019 {
3020     [[NSWorkspace sharedWorkspace] openURL: [NSURL
3021         URLWithString:@"http://handbrake.m0k.org/"]];
3022 }
3023
3024 - (IBAction) openForums: (id) sender
3025 {
3026     [[NSWorkspace sharedWorkspace] openURL: [NSURL
3027         URLWithString:@"http://handbrake.m0k.org/forum/"]];
3028 }
3029 - (IBAction) openUserGuide: (id) sender
3030 {
3031     [[NSWorkspace sharedWorkspace] openURL: [NSURL
3032         URLWithString:@"http://handbrake.m0k.org/trac/wiki/HandBrakeGuide"]];
3033 }
3034
3035 /**
3036  * Shows debug output window.
3037  */
3038 - (IBAction)showDebugOutputPanel:(id)sender
3039 {
3040     [outputPanel showOutputPanel:sender];
3041 }
3042
3043 /**
3044  * Shows preferences window.
3045  */
3046 - (IBAction) showPreferencesWindow: (id) sender
3047 {
3048     NSWindow * window = [fPreferencesController window];
3049     if (![window isVisible])
3050         [window center];
3051
3052     [window makeKeyAndOrderFront: nil];
3053 }
3054
3055 /**
3056  * Shows queue window.
3057  */
3058 - (IBAction) showQueueWindow:(id)sender
3059 {
3060     [fQueueController showQueueWindow:sender];
3061 }
3062
3063
3064 - (IBAction) toggleDrawer:(id)sender {
3065     [fPresetDrawer toggle:self];
3066 }
3067
3068 /**
3069  * Shows Picture Settings Window.
3070  */
3071
3072 - (IBAction) showPicturePanel: (id) sender
3073 {
3074         hb_list_t  * list  = hb_get_titles( fHandle );
3075     hb_title_t * title = (hb_title_t *) hb_list_item( list,
3076             [fSrcTitlePopUp indexOfSelectedItem] );
3077     [fPictureController showPanelInWindow:fWindow forTitle:title];
3078 }
3079
3080 #pragma mark -
3081 #pragma mark Preset Table View Methods
3082
3083 - (IBAction)tableViewSelected:(id)sender
3084 {
3085     /* Since we cannot disable the presets tableView in terms of clickability
3086      we will use the enabled state of the add presets button to determine whether
3087      or not clicking on a preset will do anything */
3088         if ([fPresetsAdd isEnabled])
3089         {
3090                 if ([tableView selectedRow] >= 0)
3091                 {       
3092                         /* we get the chosen preset from the UserPresets array */
3093                         chosenPreset = [UserPresets objectAtIndex:[tableView selectedRow]];
3094                         curUserPresetChosenNum = [sender selectedRow];
3095                         /* we set the preset display field in main window here */
3096                         [fPresetSelectedDisplay setStringValue: [NSString stringWithFormat: @"%@",[chosenPreset valueForKey:@"PresetName"]]];
3097                         if ([[chosenPreset objectForKey:@"Default"] intValue] == 1)
3098                         {
3099                                 [fPresetSelectedDisplay setStringValue: [NSString stringWithFormat: @"%@ (Default)",[chosenPreset valueForKey:@"PresetName"]]];
3100                         }
3101                         else
3102                         {
3103                                 [fPresetSelectedDisplay setStringValue: [NSString stringWithFormat: @"%@",[chosenPreset valueForKey:@"PresetName"]]];
3104                         }
3105                         /* File Format */
3106                         [fDstFormatPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"FileFormat"]]];
3107                         [self formatPopUpChanged: NULL];
3108                         
3109                         /* Chapter Markers*/
3110                         [fCreateChapterMarkers setState:[[chosenPreset objectForKey:@"ChapterMarkers"] intValue]];
3111                         /* Allow Mpeg4 64 bit formatting +4GB file sizes */
3112                         [fDstMpgLargeFileCheck setState:[[chosenPreset objectForKey:@"Mp4LargeFile"] intValue]];
3113                         /* Codecs */
3114                         [fDstCodecsPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"FileCodecs"]]];
3115                         [self codecsPopUpChanged: NULL];
3116                         /* Video encoder */
3117                         [fVidEncoderPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoEncoder"]]];
3118                         
3119                         /* We can show the preset options here in the gui if we want to
3120              so we check to see it the user has specified it in the prefs */
3121                         [fAdvancedOptions setOptions: [NSString stringWithFormat:[chosenPreset valueForKey:@"x264Option"]]];
3122                         
3123                         /* Lets run through the following functions to get variables set there */
3124                         [self encoderPopUpChanged: NULL];
3125                         
3126                         [self calculateBitrate: NULL];
3127                         
3128                         /* Video quality */
3129                         [fVidQualityMatrix selectCellAtRow:[[chosenPreset objectForKey:@"VideoQualityType"] intValue] column:0];
3130                         
3131                         [fVidTargetSizeField setStringValue: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoTargetSize"]]];
3132                         [fVidBitrateField setStringValue: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoAvgBitrate"]]];
3133                         
3134                         [fVidQualitySlider setFloatValue: [[chosenPreset valueForKey:@"VideoQualitySlider"] floatValue]];
3135                         [self videoMatrixChanged: NULL];
3136                         
3137                         /* Video framerate */
3138                         /* For video preset video framerate, we want to make sure that Same as source does not conflict with the
3139              detected framerate in the fVidRatePopUp so we use index 0*/
3140                         if ([[NSString stringWithFormat:[chosenPreset valueForKey:@"VideoFramerate"]] isEqualToString: @"Same as source"])
3141             {
3142                 [fVidRatePopUp selectItemAtIndex: 0];
3143             }
3144             else
3145             {
3146                 [fVidRatePopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoFramerate"]]];
3147             }
3148                         
3149                         /* GrayScale */
3150                         [fVidGrayscaleCheck setState:[[chosenPreset objectForKey:@"VideoGrayScale"] intValue]];
3151                         
3152                         /* 2 Pass Encoding */
3153                         [fVidTwoPassCheck setState:[[chosenPreset objectForKey:@"VideoTwoPass"] intValue]];
3154                         [self twoPassCheckboxChanged: NULL];
3155                         /* Turbo 1st pass for 2 Pass Encoding */
3156                         [fVidTurboPassCheck setState:[[chosenPreset objectForKey:@"VideoTurboTwoPass"] intValue]];
3157                         
3158                         /*Audio*/
3159                         
3160                         /* Audio Sample Rate*/
3161                         [fAudRatePopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"AudioSampleRate"]]];
3162                         /* Audio Bitrate Rate*/
3163                         [fAudBitratePopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"AudioBitRate"]]];
3164                         /*Subtitles*/
3165                         [fSubPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"Subtitles"]]];
3166                         
3167                         /* Picture Settings */
3168                         /* Note: objectForKey:@"UsesPictureSettings" now refers to picture size, this encompasses:
3169              * height, width, keep ar, anamorphic and crop settings.
3170              * picture filters are now handled separately.
3171              * We will be able to actually change the key names for legacy preset keys when preset file
3172              * update code is done. But for now, lets hang onto the old legacy key name for backwards compatibility.
3173              */
3174                         /* Check to see if the objectForKey:@"UsesPictureSettings is greater than 0, as 0 means use picture sizing "None" 
3175              * and the preset completely ignores any picture sizing values in the preset.
3176              */
3177             if ([[chosenPreset objectForKey:@"UsesPictureSettings"]  intValue] > 0)
3178                         {
3179                                 hb_job_t * job = fTitle->job;
3180                                 /* Check to see if the objectForKey:@"UsesPictureSettings is 2 which is "Use Max for the source */
3181                                 if ([[chosenPreset objectForKey:@"UsesPictureSettings"]  intValue] == 2 || [[chosenPreset objectForKey:@"UsesMaxPictureSettings"]  intValue] == 1)
3182                                 {
3183                                         /* Use Max Picture settings for whatever the dvd is.*/
3184                                         [self revertPictureSizeToMax: NULL];
3185                                         job->keep_ratio = [[chosenPreset objectForKey:@"PictureKeepRatio"]  intValue];
3186                                         if (job->keep_ratio == 1)
3187                                         {
3188                                                 hb_fix_aspect( job, HB_KEEP_WIDTH );
3189                                                 if( job->height > fTitle->height )
3190                                                 {
3191                                                         job->height = fTitle->height;
3192                                                         hb_fix_aspect( job, HB_KEEP_HEIGHT );
3193                                                 }
3194                                         }
3195                                         job->pixel_ratio = [[chosenPreset objectForKey:@"PicturePAR"]  intValue];
3196                                 }
3197                                 else // /* If not 0 or 2 we assume objectForKey:@"UsesPictureSettings is 1 which is "Use picture sizing from when the preset was set" */
3198                                 {
3199                                         job->width = [[chosenPreset objectForKey:@"PictureWidth"]  intValue];
3200                                         job->height = [[chosenPreset objectForKey:@"PictureHeight"]  intValue];
3201                                         job->keep_ratio = [[chosenPreset objectForKey:@"PictureKeepRatio"]  intValue];
3202                                         if (job->keep_ratio == 1)
3203                                         {
3204                                                 hb_fix_aspect( job, HB_KEEP_WIDTH );
3205                                                 if( job->height > fTitle->height )
3206                                                 {
3207                                                         job->height = fTitle->height;
3208                                                         hb_fix_aspect( job, HB_KEEP_HEIGHT );
3209                                                 }
3210                                         }
3211                                         job->pixel_ratio = [[chosenPreset objectForKey:@"PicturePAR"]  intValue];
3212                     
3213                     
3214                                         /* If Cropping is set to custom, then recall all four crop values from
3215                      when the preset was created and apply them */
3216                                         if ([[chosenPreset objectForKey:@"PictureAutoCrop"]  intValue] == 0)
3217                                         {
3218                         [fPictureController setAutoCrop:NO];
3219                                                 
3220                                                 /* Here we use the custom crop values saved at the time the preset was saved */
3221                                                 job->crop[0] = [[chosenPreset objectForKey:@"PictureTopCrop"]  intValue];
3222                                                 job->crop[1] = [[chosenPreset objectForKey:@"PictureBottomCrop"]  intValue];
3223                                                 job->crop[2] = [[chosenPreset objectForKey:@"PictureLeftCrop"]  intValue];
3224                                                 job->crop[3] = [[chosenPreset objectForKey:@"PictureRightCrop"]  intValue];
3225                                                 
3226                                         }
3227                                         else /* if auto crop has been saved in preset, set to auto and use post scan auto crop */
3228                                         {
3229                         [fPictureController setAutoCrop:YES];
3230                         /* Here we use the auto crop values determined right after scan */
3231                                                 job->crop[0] = AutoCropTop;
3232                                                 job->crop[1] = AutoCropBottom;
3233                                                 job->crop[2] = AutoCropLeft;
3234                                                 job->crop[3] = AutoCropRight;
3235                                                 
3236                                         }
3237                     /* If the preset has no objectForKey:@"UsesPictureFilters", then we know it is a legacy preset
3238                      * and handle the filters here as before.
3239                      * NOTE: This should be removed when the update presets code is done as we can be assured that legacy
3240                      * presets are updated to work properly with new keys.
3241                      */
3242                     if (![chosenPreset objectForKey:@"UsesPictureFilters"])
3243                     {
3244                         /* Filters */
3245                         [fPictureController setDeinterlace:[[chosenPreset objectForKey:@"PictureDeinterlace"] intValue]];
3246                         
3247                         if ([[chosenPreset objectForKey:@"VFR"] intValue] == 1)
3248                         {
3249                             [fPictureController setVFR:[[chosenPreset objectForKey:@"VFR"] intValue]];
3250                         }
3251                         else
3252                         {
3253                         [fPictureController setVFR:0];
3254                         }
3255                         
3256                         if ([chosenPreset objectForKey:@"PictureDetelecine"])
3257                         {
3258                             [fPictureController setDetelecine:[[chosenPreset objectForKey:@"PictureDetelecine"] intValue]];
3259                         }
3260                         if ([chosenPreset objectForKey:@"PictureDenoise"])
3261                         {
3262                             [fPictureController setDenoise:[[chosenPreset objectForKey:@"PictureDenoise"] intValue]];
3263                         }
3264                         if ([chosenPreset objectForKey:@"PictureDeblock"])
3265                         {
3266                             [fPictureController setDeblock:[[chosenPreset objectForKey:@"PictureDeblock"] intValue]];
3267                         }   
3268                    [self calculatePictureSizing: NULL];
3269                      }
3270                     
3271                                 }
3272                 
3273                 
3274                         }
3275                         /* If the preset has an objectForKey:@"UsesPictureFilters", then we know it is a newer style filters preset
3276             * and handle the filters here depending on whether or not the preset specifies applying the filter.
3277             */
3278             if ([chosenPreset objectForKey:@"UsesPictureFilters"] && [[chosenPreset objectForKey:@"UsesPictureFilters"]  intValue] > 0)
3279             {
3280                 /* Filters */
3281                 [fPictureController setDeinterlace:[[chosenPreset objectForKey:@"PictureDeinterlace"] intValue]];
3282                 if ([chosenPreset objectForKey:@"VFR"])
3283                 {
3284                     [fPictureController setVFR:[[chosenPreset objectForKey:@"VFR"] intValue]];
3285                 }
3286                 else
3287                 {
3288                 [fPictureController setVFR:0];
3289                 }
3290                 if ([chosenPreset objectForKey:@"PictureDetelecine"])
3291                 {
3292                     [fPictureController setDetelecine:[[chosenPreset objectForKey:@"PictureDetelecine"] intValue]];
3293                 }
3294                 if ([chosenPreset objectForKey:@"PictureDenoise"])
3295                 {
3296                     [fPictureController setDenoise:[[chosenPreset objectForKey:@"PictureDenoise"] intValue]];
3297                 }
3298                 if ([chosenPreset objectForKey:@"PictureDeblock"])
3299                 {
3300                     [fPictureController setDeblock:[[chosenPreset objectForKey:@"PictureDeblock"] intValue]];
3301                 }   
3302             }
3303                         [self calculatePictureSizing: NULL];
3304                         [[fPresetsActionMenu itemAtIndex:0] setEnabled: YES];
3305         }
3306     }
3307 }
3308
3309
3310
3311 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
3312 {
3313     return [UserPresets count];
3314 }
3315
3316 /* we use this to determine display characteristics for
3317 each table cell based on content currently only used to
3318 show the built in presets in a blue font. */
3319 - (void)tableView:(NSTableView *)aTableView
3320  willDisplayCell:(id)aCell 
3321  forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
3322 {
3323     NSDictionary *userPresetDict = [UserPresets objectAtIndex:rowIndex];
3324         NSFont *txtFont;
3325         NSColor *fontColor;
3326         NSColor *shadowColor;
3327         txtFont = [NSFont systemFontOfSize: [NSFont smallSystemFontSize]];
3328         /* First, we check to see if its a selected row, if so, we use white since its highlighted in blue */
3329         if ([[aTableView selectedRowIndexes] containsIndex:rowIndex] && ([tableView editedRow] != rowIndex))
3330         {
3331                 
3332                 fontColor = [NSColor whiteColor];
3333                 shadowColor = [NSColor colorWithDeviceRed:(127.0/255.0) green:(140.0/255.0) blue:(160.0/255.0) alpha:1.0];
3334         }
3335         else
3336         {
3337                 /* We set the properties of unselected rows */
3338                 /* if built-in preset (defined by "type" == 0) we use a blue font */
3339                 if ([[userPresetDict objectForKey:@"Type"] intValue] == 0)
3340                 {
3341                         fontColor = [NSColor blueColor];
3342                 }
3343                 else // User created preset, use a black font
3344                 {
3345                         fontColor = [NSColor blackColor];
3346                 }
3347                 shadowColor = nil;
3348         }
3349         /* We check to see if this is the HB default, if so, color it appropriately */
3350         if (!presetUserDefault && presetHbDefault && rowIndex == presetHbDefault)
3351         {
3352         txtFont = [NSFont boldSystemFontOfSize: [NSFont smallSystemFontSize]];
3353         }
3354         /* We check to see if this is the User Specified default, if so, color it appropriately */
3355         if (presetUserDefault && rowIndex == presetUserDefault)
3356         {
3357         txtFont = [NSFont boldSystemFontOfSize: [NSFont smallSystemFontSize]];
3358         }
3359         
3360         [aCell setTextColor:fontColor];
3361         [aCell setFont:txtFont];
3362         /* this shadow stuff (like mail app) for some reason looks crappy, commented out
3363         temporarily in case we want to resurrect it */
3364         /*
3365         NSShadow *shadow = [[NSShadow alloc] init];
3366         NSSize shadowOffset = { width: 1.0, height: -1.5};
3367         [shadow setShadowOffset:shadowOffset];
3368         [shadow setShadowColor:shadowColor];
3369         [shadow set];
3370         */
3371         
3372 }
3373 /* Method to display tooltip with the description for each preset, if available */
3374 - (NSString *)tableView:(NSTableView *)aTableView toolTipForCell:(NSCell *)aCell 
3375                    rect:(NSRectPointer)aRect tableColumn:(NSTableColumn *)aTableColumn
3376                     row:(int)rowIndex mouseLocation:(NSPoint)aPos
3377 {
3378      /* initialize the tooltip contents variable */
3379          NSString *loc_tip;
3380      /* if there is a description for the preset, we show it in the tooltip */
3381          if ([[UserPresets objectAtIndex:rowIndex] valueForKey:@"PresetDescription"])
3382          {
3383         loc_tip = [NSString stringWithFormat: @"%@",[[UserPresets objectAtIndex:rowIndex] valueForKey:@"PresetDescription"]];
3384         return (loc_tip);
3385          }
3386          else
3387          {
3388         loc_tip = @"No description available";
3389          }
3390          return (loc_tip);
3391
3392 }
3393
3394 - (id)tableView:(NSTableView *)aTableView
3395       objectValueForTableColumn:(NSTableColumn *)aTableColumn
3396       row:(int)rowIndex
3397 {
3398 id theRecord, theValue;
3399     
3400     theRecord = [UserPresets objectAtIndex:rowIndex];
3401     theValue = [theRecord objectForKey:[aTableColumn identifier]];
3402     return theValue;
3403 }
3404
3405 // NSTableDataSource method that we implement to edit values directly in the table...
3406 - (void)tableView:(NSTableView *)aTableView
3407         setObjectValue:(id)anObject
3408         forTableColumn:(NSTableColumn *)aTableColumn
3409         row:(int)rowIndex
3410 {
3411     id theRecord;
3412     
3413     theRecord = [UserPresets objectAtIndex:rowIndex];
3414     [theRecord setObject:anObject forKey:@"PresetName"];
3415     /* We Sort the Presets By Factory or Custom */
3416         NSSortDescriptor * presetTypeDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"Type" 
3417                                                     ascending:YES] autorelease];
3418                 /* We Sort the Presets Alphabetically by name */
3419         NSSortDescriptor * presetNameDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"PresetName" 
3420                                                     ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease];
3421         NSArray *sortDescriptors=[NSArray arrayWithObjects:presetTypeDescriptor,presetNameDescriptor,nil];
3422     NSArray *sortedArray=[UserPresets sortedArrayUsingDescriptors:sortDescriptors];
3423         [UserPresets setArray:sortedArray];
3424         /* We Reload the New Table data for presets */
3425     [tableView reloadData];
3426    /* We save all of the preset data here */
3427     [self savePreset];
3428 }
3429
3430
3431
3432
3433 #pragma mark -
3434 #pragma mark Manage Presets
3435
3436 - (void) loadPresets {
3437         /* We declare the default NSFileManager into fileManager */
3438         NSFileManager * fileManager = [NSFileManager defaultManager];
3439         /*We define the location of the user presets file */
3440     UserPresetsFile = @"~/Library/Application Support/HandBrake/UserPresets.plist";
3441         UserPresetsFile = [[UserPresetsFile stringByExpandingTildeInPath]retain];
3442     /* We check for the presets.plist */
3443         if ([fileManager fileExistsAtPath:UserPresetsFile] == 0) 
3444         {
3445                 [fileManager createFileAtPath:UserPresetsFile contents:nil attributes:nil];
3446         }
3447                 
3448         UserPresets = [[NSMutableArray alloc] initWithContentsOfFile:UserPresetsFile];
3449         if (nil == UserPresets) 
3450         {
3451                 UserPresets = [[NSMutableArray alloc] init];
3452                 [self addFactoryPresets:NULL];
3453         }
3454         
3455 }
3456
3457
3458 - (IBAction) showAddPresetPanel: (id) sender
3459 {
3460     /* Deselect the currently selected Preset if there is one*/
3461     [tableView deselectRow:[tableView selectedRow]];
3462
3463     /* Populate the preset picture settings popup here */
3464     [fPresetNewPicSettingsPopUp removeAllItems];
3465     [fPresetNewPicSettingsPopUp addItemWithTitle:@"None"];
3466     [fPresetNewPicSettingsPopUp addItemWithTitle:@"Current"];
3467     [fPresetNewPicSettingsPopUp addItemWithTitle:@"Source Maximum (post source scan)"];
3468     [fPresetNewPicSettingsPopUp selectItemAtIndex: 0];  
3469     /* Uncheck the preset use filters checkbox */
3470     [fPresetNewPicFiltersCheck setState:NSOffState];
3471     /* Erase info from the input fields*/
3472         [fPresetNewName setStringValue: @""];
3473         [fPresetNewDesc setStringValue: @""];
3474         /* Show the panel */
3475         [NSApp beginSheet: fAddPresetPanel modalForWindow: fWindow modalDelegate: NULL didEndSelector: NULL contextInfo: NULL];
3476 }
3477
3478 - (IBAction) closeAddPresetPanel: (id) sender
3479 {
3480     [NSApp endSheet: fAddPresetPanel];
3481     [fAddPresetPanel orderOut: self];
3482 }
3483
3484 - (IBAction)addUserPreset:(id)sender
3485 {
3486     if (![[fPresetNewName stringValue] length])
3487             NSRunAlertPanel(@"Warning!", @"You need to insert a name for the preset.", @"OK", nil , nil);
3488     else
3489     {
3490         /* Here we create a custom user preset */
3491         [UserPresets addObject:[self createPreset]];
3492         [self addPreset];
3493         
3494         [self closeAddPresetPanel:NULL];
3495     }
3496 }
3497 - (void)addPreset
3498 {
3499
3500         
3501         /* We Sort the Presets By Factory or Custom */
3502         NSSortDescriptor * presetTypeDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"Type" 
3503                                                     ascending:YES] autorelease];
3504         /* We Sort the Presets Alphabetically by name */
3505         NSSortDescriptor * presetNameDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"PresetName" 
3506                                                     ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease];
3507         NSArray *sortDescriptors=[NSArray arrayWithObjects:presetTypeDescriptor,presetNameDescriptor,nil];
3508         NSArray *sortedArray=[UserPresets sortedArrayUsingDescriptors:sortDescriptors];
3509         [UserPresets setArray:sortedArray];
3510         
3511         
3512         /* We Reload the New Table data for presets */
3513     [tableView reloadData];
3514    /* We save all of the preset data here */
3515     [self savePreset];
3516 }
3517
3518 - (IBAction)insertPreset:(id)sender
3519 {
3520     int index = [tableView selectedRow];
3521     [UserPresets insertObject:[self createPreset] atIndex:index];
3522     [tableView reloadData];
3523     [self savePreset];
3524 }
3525
3526 - (NSDictionary *)createPreset
3527 {
3528     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3529         /* Get the New Preset Name from the field in the AddPresetPanel */
3530     [preset setObject:[fPresetNewName stringValue] forKey:@"PresetName"];
3531         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3532         [preset setObject:[NSNumber numberWithInt:1] forKey:@"Type"];
3533         /*Set whether or not this is default, at creation set to 0*/
3534         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3535         /*Get the whether or not to apply pic Size and Cropping (includes Anamorphic)*/
3536         [preset setObject:[NSNumber numberWithInt:[fPresetNewPicSettingsPopUp indexOfSelectedItem]] forKey:@"UsesPictureSettings"];
3537     /* Get whether or not to use the current Picture Filter settings for the preset */
3538     [preset setObject:[NSNumber numberWithInt:[fPresetNewPicFiltersCheck state]] forKey:@"UsesPictureFilters"];
3539  
3540     /* Get New Preset Description from the field in the AddPresetPanel*/
3541         [preset setObject:[fPresetNewDesc stringValue] forKey:@"PresetDescription"];
3542         /* File Format */
3543     [preset setObject:[fDstFormatPopUp titleOfSelectedItem] forKey:@"FileFormat"];
3544         /* Chapter Markers fCreateChapterMarkers*/
3545         [preset setObject:[NSNumber numberWithInt:[fCreateChapterMarkers state]] forKey:@"ChapterMarkers"];
3546         /* Allow Mpeg4 64 bit formatting +4GB file sizes */
3547         [preset setObject:[NSNumber numberWithInt:[fDstMpgLargeFileCheck state]] forKey:@"Mp4LargeFile"];
3548         /* Codecs */
3549         [preset setObject:[fDstCodecsPopUp titleOfSelectedItem] forKey:@"FileCodecs"];
3550         /* Video encoder */
3551         [preset setObject:[fVidEncoderPopUp titleOfSelectedItem] forKey:@"VideoEncoder"];
3552         /* x264 Option String */
3553         [preset setObject:[fAdvancedOptions optionsString] forKey:@"x264Option"];
3554         
3555         [preset setObject:[NSNumber numberWithInt:[fVidQualityMatrix selectedRow]] forKey:@"VideoQualityType"];
3556         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3557         [preset setObject:[fVidBitrateField stringValue] forKey:@"VideoAvgBitrate"];
3558         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3559         
3560         /* Video framerate */
3561     if ([fVidRatePopUp indexOfSelectedItem] == 0) // Same as source is selected
3562         {
3563     [preset setObject:[NSString stringWithFormat: @"Same as source"] forKey:@"VideoFramerate"];
3564     }
3565     else // we can record the actual titleOfSelectedItem
3566     {
3567     [preset setObject:[fVidRatePopUp titleOfSelectedItem] forKey:@"VideoFramerate"];
3568     }
3569         /* GrayScale */
3570         [preset setObject:[NSNumber numberWithInt:[fVidGrayscaleCheck state]] forKey:@"VideoGrayScale"];
3571         /* 2 Pass Encoding */
3572         [preset setObject:[NSNumber numberWithInt:[fVidTwoPassCheck state]] forKey:@"VideoTwoPass"];
3573         /* Turbo 2 pass Encoding fVidTurboPassCheck*/
3574         [preset setObject:[NSNumber numberWithInt:[fVidTurboPassCheck state]] forKey:@"VideoTurboTwoPass"];
3575         /*Picture Settings*/
3576         hb_job_t * job = fTitle->job;
3577         /* Picture Sizing */
3578         /* Use Max Picture settings for whatever the dvd is.*/
3579         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3580         [preset setObject:[NSNumber numberWithInt:fTitle->job->width] forKey:@"PictureWidth"];
3581         [preset setObject:[NSNumber numberWithInt:fTitle->job->height] forKey:@"PictureHeight"];
3582         [preset setObject:[NSNumber numberWithInt:fTitle->job->keep_ratio] forKey:@"PictureKeepRatio"];
3583         [preset setObject:[NSNumber numberWithInt:fTitle->job->pixel_ratio] forKey:@"PicturePAR"];
3584     
3585     /* Set crop settings here */
3586         [preset setObject:[NSNumber numberWithInt:[fPictureController autoCrop]] forKey:@"PictureAutoCrop"];
3587     [preset setObject:[NSNumber numberWithInt:job->crop[0]] forKey:@"PictureTopCrop"];
3588     [preset setObject:[NSNumber numberWithInt:job->crop[1]] forKey:@"PictureBottomCrop"];
3589         [preset setObject:[NSNumber numberWithInt:job->crop[2]] forKey:@"PictureLeftCrop"];
3590         [preset setObject:[NSNumber numberWithInt:job->crop[3]] forKey:@"PictureRightCrop"];
3591     
3592     /* Picture Filters */
3593     [preset setObject:[NSNumber numberWithInt:[fPictureController deinterlace]] forKey:@"PictureDeinterlace"];
3594         [preset setObject:[NSNumber numberWithInt:[fPictureController detelecine]] forKey:@"PictureDetelecine"];
3595     [preset setObject:[NSNumber numberWithInt:[fPictureController vfr]] forKey:@"VFR"];
3596         [preset setObject:[NSNumber numberWithInt:[fPictureController denoise]] forKey:@"PictureDenoise"];
3597     [preset setObject:[NSNumber numberWithInt:[fPictureController deblock]] forKey:@"PictureDeblock"];
3598     
3599
3600         
3601         /*Audio*/
3602         /* Audio Sample Rate*/
3603         [preset setObject:[fAudRatePopUp titleOfSelectedItem] forKey:@"AudioSampleRate"];
3604         /* Audio Bitrate Rate*/
3605         [preset setObject:[fAudBitratePopUp titleOfSelectedItem] forKey:@"AudioBitRate"];
3606         /* Subtitles*/
3607         [preset setObject:[fSubPopUp titleOfSelectedItem] forKey:@"Subtitles"];
3608         
3609
3610     [preset autorelease];
3611     return preset;
3612
3613 }
3614
3615 - (void)savePreset
3616 {
3617     [UserPresets writeToFile:UserPresetsFile atomically:YES];
3618         /* We get the default preset in case it changed */
3619         [self getDefaultPresets: NULL];
3620
3621 }
3622
3623 - (IBAction)deletePreset:(id)sender
3624 {
3625     int status;
3626     NSEnumerator *enumerator;
3627     NSNumber *index;
3628     NSMutableArray *tempArray;
3629     id tempObject;
3630     
3631     if ( [tableView numberOfSelectedRows] == 0 )
3632         return;
3633     /* Alert user before deleting preset */
3634         /* Comment out for now, tie to user pref eventually */
3635
3636     //NSBeep();
3637     status = NSRunAlertPanel(@"Warning!", @"Are you sure that you want to delete the selected preset?", @"OK", @"Cancel", nil);
3638     
3639     if ( status == NSAlertDefaultReturn ) {
3640         enumerator = [tableView selectedRowEnumerator];
3641         tempArray = [NSMutableArray array];
3642         
3643         while ( (index = [enumerator nextObject]) ) {
3644             tempObject = [UserPresets objectAtIndex:[index intValue]];
3645             [tempArray addObject:tempObject];
3646         }
3647         
3648         [UserPresets removeObjectsInArray:tempArray];
3649         [tableView reloadData];
3650         [self savePreset];   
3651     }
3652 }
3653
3654 #pragma mark -
3655 #pragma mark Manage Default Preset
3656
3657 - (IBAction)getDefaultPresets:(id)sender
3658 {
3659         int i = 0;
3660     NSEnumerator *enumerator = [UserPresets objectEnumerator];
3661         id tempObject;
3662         while (tempObject = [enumerator nextObject])
3663         {
3664                 NSDictionary *thisPresetDict = tempObject;
3665                 if ([[thisPresetDict objectForKey:@"Default"] intValue] == 1) // 1 is HB default
3666                 {
3667                         presetHbDefault = i;    
3668                 }
3669                 if ([[thisPresetDict objectForKey:@"Default"] intValue] == 2) // 2 is User specified default
3670                 {
3671                         presetUserDefault = i;  
3672                 }
3673                 i++;
3674         }
3675 }
3676
3677 - (IBAction)setDefaultPreset:(id)sender
3678 {
3679     int i = 0;
3680     NSEnumerator *enumerator = [UserPresets objectEnumerator];
3681         id tempObject;
3682         /* First make sure the old user specified default preset is removed */
3683         while (tempObject = [enumerator nextObject])
3684         {
3685                 /* make sure we are not removing the default HB preset */
3686                 if ([[[UserPresets objectAtIndex:i] objectForKey:@"Default"] intValue] != 1) // 1 is HB default
3687                 {
3688                         [[UserPresets objectAtIndex:i] setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3689                 }
3690                 i++;
3691         }
3692         /* Second, go ahead and set the appropriate user specfied preset */
3693         /* we get the chosen preset from the UserPresets array */
3694         if ([[[UserPresets objectAtIndex:[tableView selectedRow]] objectForKey:@"Default"] intValue] != 1) // 1 is HB default
3695         {
3696                 [[UserPresets objectAtIndex:[tableView selectedRow]] setObject:[NSNumber numberWithInt:2] forKey:@"Default"];
3697         }
3698         presetUserDefault = [tableView selectedRow];
3699         
3700         /* We save all of the preset data here */
3701     [self savePreset];
3702         /* We Reload the New Table data for presets */
3703     [tableView reloadData];
3704 }
3705
3706 - (IBAction)selectDefaultPreset:(id)sender
3707 {
3708         /* if there is a user specified default, we use it */
3709         if (presetUserDefault)
3710         {
3711         [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:presetUserDefault] byExtendingSelection:NO];
3712         [self tableViewSelected:NULL];
3713         }
3714         else if (presetHbDefault) //else we use the built in default presetHbDefault
3715         {
3716         [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:presetHbDefault] byExtendingSelection:NO];
3717         [self tableViewSelected:NULL];
3718         }
3719 }
3720
3721
3722 #pragma mark -
3723 #pragma mark Manage Built In Presets
3724
3725
3726 - (IBAction)deleteFactoryPresets:(id)sender
3727 {
3728     //int status;
3729     NSEnumerator *enumerator = [UserPresets objectEnumerator];
3730         id tempObject;
3731     
3732         //NSNumber *index;
3733     NSMutableArray *tempArray;
3734
3735
3736         tempArray = [NSMutableArray array];
3737         /* we look here to see if the preset is we move on to the next one */
3738         while ( tempObject = [enumerator nextObject] )  
3739                 {
3740                         /* if the preset is "Factory" then we put it in the array of
3741                         presets to delete */
3742                         if ([[tempObject objectForKey:@"Type"] intValue] == 0)
3743                         {
3744                                 [tempArray addObject:tempObject];
3745                         }
3746         }
3747         
3748         [UserPresets removeObjectsInArray:tempArray];
3749         [tableView reloadData];
3750         [self savePreset];   
3751
3752 }
3753
3754    /* We use this method to recreate new, updated factory
3755    presets */
3756 - (IBAction)addFactoryPresets:(id)sender
3757 {
3758     /* First, we delete any existing built in presets */
3759     [self deleteFactoryPresets: sender];
3760     /* Then, we re-create new built in presets programmatically CreateIpodOnlyPreset*/
3761     [UserPresets addObject:[self createNormalPreset]];
3762     [UserPresets addObject:[self createClassicPreset]];
3763     [UserPresets addObject:[self createQuickTimePreset]];
3764         [UserPresets addObject:[self createIpodLowPreset]];
3765         [UserPresets addObject:[self createIpodHighPreset]];
3766         [UserPresets addObject:[self createAppleTVPreset]];
3767     [UserPresets addObject:[self createiPhonePreset]];
3768         [UserPresets addObject:[self createPSThreePreset]];
3769         [UserPresets addObject:[self createPSPPreset]];
3770         [UserPresets addObject:[self createFilmPreset]];
3771     [UserPresets addObject:[self createTelevisionPreset]];
3772     [UserPresets addObject:[self createAnimationPreset]];
3773     [UserPresets addObject:[self createBedlamPreset]];
3774     [UserPresets addObject:[self createDeuxSixQuatrePreset]];
3775     [UserPresets addObject:[self createBrokePreset]];
3776     [UserPresets addObject:[self createBlindPreset]];
3777     [UserPresets addObject:[self createCRFPreset]];
3778     
3779     [self addPreset];
3780 }
3781
3782 #pragma mark -
3783 #pragma mark Built In Preset Definitions
3784 /* These NSDictionary Buit In Preset definitions contain all of the settings for one built in preset */
3785 - (NSDictionary *)createIpodLowPreset
3786 {
3787     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3788         /* Get the New Preset Name from the field in the AddPresetPanel */
3789     [preset setObject:@"iPod Low-Rez" forKey:@"PresetName"];
3790         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3791         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3792         /*Set whether or not this is default, at creation set to 0*/
3793         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3794         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3795         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3796         /* Get the New Preset Description from the field in the AddPresetPanel */
3797     [preset setObject:@"HandBrake's low resolution settings for the iPod. Optimized for great playback on the iPod screen, with smaller file size." forKey:@"PresetDescription"];
3798         /* File Format */
3799     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3800         /* Chapter Markers*/
3801          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3802     /* Codecs */
3803         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3804         /* Video encoder */
3805         [preset setObject:@"x264 (h.264 iPod)" forKey:@"VideoEncoder"];
3806         /* x264 Option String */
3807         [preset setObject:@"keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1" forKey:@"x264Option"];
3808         /* Video quality */
3809         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3810         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3811         [preset setObject:@"700" forKey:@"VideoAvgBitrate"];
3812         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3813         
3814         /* Video framerate */
3815         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3816         /* GrayScale */
3817         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3818         /* 2 Pass Encoding */
3819         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3820         
3821         /*Picture Settings*/
3822         //hb_job_t * job = fTitle->job;
3823         /* Basic Picture Settings */
3824         /* Use Max Picture settings for whatever the dvd is.*/
3825         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3826         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3827         [preset setObject:[NSNumber numberWithInt:320] forKey:@"PictureWidth"];
3828         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3829         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3830         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3831         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3832         /* Set crop settings here */
3833         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3834         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3835     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3836         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3837         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3838         
3839         /*Audio*/
3840         /* Audio Sample Rate*/
3841         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3842         /* Audio Bitrate Rate*/
3843         [preset setObject:@"160" forKey:@"AudioBitRate"];
3844         /* Subtitles*/
3845         [preset setObject:@"None" forKey:@"Subtitles"];
3846         
3847
3848     [preset autorelease];
3849     return preset;
3850
3851 }
3852
3853
3854 - (NSDictionary *)createIpodHighPreset
3855 {
3856     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3857         /* Get the New Preset Name from the field in the AddPresetPanel */
3858     [preset setObject:@"iPod High-Rez" forKey:@"PresetName"];
3859         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3860         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3861         /*Set whether or not this is default, at creation set to 0*/
3862         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3863         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3864         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3865         /* Get the New Preset Description from the field in the AddPresetPanel */
3866     [preset setObject:@"HandBrake's high resolution settings for the iPod. Good video quality, great for viewing on a TV using your iPod" forKey:@"PresetDescription"];
3867         /* File Format */
3868     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3869         /* Chapter Markers*/
3870          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3871     /* Codecs */
3872         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3873         /* Video encoder */
3874         [preset setObject:@"x264 (h.264 iPod)" forKey:@"VideoEncoder"];
3875         /* x264 Option String */
3876         [preset setObject:@"keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1" forKey:@"x264Option"];
3877         /* Video quality */
3878         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3879         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3880         [preset setObject:@"1500" forKey:@"VideoAvgBitrate"];
3881         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3882         
3883         /* Video framerate */
3884         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3885         /* GrayScale */
3886         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3887         /* 2 Pass Encoding */
3888         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3889         
3890         /*Picture Settings*/
3891         //hb_job_t * job = fTitle->job;
3892         /* Basic Picture Settings */
3893         /* Use Max Picture settings for whatever the dvd is.*/
3894         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3895         [preset setObject:[NSNumber numberWithInt:640] forKey:@"PictureWidth"];
3896         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3897         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3898         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3899         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3900         /* Set crop settings here */
3901         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3902         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3903         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3904     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3905         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3906         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3907         
3908         /*Audio*/
3909         /* Audio Sample Rate*/
3910         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3911         /* Audio Bitrate Rate*/
3912         [preset setObject:@"160" forKey:@"AudioBitRate"];
3913         /* Subtitles*/
3914         [preset setObject:@"None" forKey:@"Subtitles"];
3915         
3916
3917     [preset autorelease];
3918     return preset;
3919
3920 }
3921
3922 - (NSDictionary *)createAppleTVPreset
3923 {
3924     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3925         /* Get the New Preset Name from the field in the AddPresetPanel */
3926     [preset setObject:@"AppleTV" forKey:@"PresetName"];
3927         /*Set whether or not this is a user preset where 0 is factory, 1 is user*/
3928         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3929         /*Set whether or not this is default, at creation set to 0*/
3930         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3931         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3932         [preset setObject:[NSNumber numberWithInt:2] forKey:@"UsesPictureSettings"];
3933         /* Get the New Preset Description from the field in the AddPresetPanel */
3934     [preset setObject:@"HandBrake's settings for the AppleTV. Provides a good balance between quality and file size, and optimizes performance." forKey:@"PresetDescription"];
3935         /* File Format */
3936     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3937         /* Chapter Markers*/
3938          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3939         /* Codecs */
3940         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3941         /* Video encoder */
3942         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3943         /* x264 Option String (We can use this to tweak the appleTV output)*/
3944         [preset setObject:@"bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=2:cabac=0" forKey:@"x264Option"];
3945         /* Video quality */
3946         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3947         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3948         [preset setObject:@"2500" forKey:@"VideoAvgBitrate"];
3949         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3950         
3951         /* Video framerate */
3952         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3953         /* GrayScale */
3954         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3955         /* 2 Pass Encoding */
3956         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3957         
3958         /*Picture Settings*/
3959         /* For AppleTV we only want to retain UsesMaxPictureSettings
3960         which depend on the source dvd picture settings, so we don't
3961         record the current dvd's picture info since it will vary from
3962         source to source*/
3963         //hb_job_t * job = fTitle->job;
3964         //hb_job_t * job = title->job;
3965         /* Basic Picture Settings */
3966         /* Use Max Picture settings for whatever the dvd is.*/
3967         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3968         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3969         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3970         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3971         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3972         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3973         /* Set crop settings here */
3974         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3975         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3976     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3977         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3978         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3979         
3980         /*Audio*/
3981         /* Audio Sample Rate*/
3982         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3983         /* Audio Bitrate Rate*/
3984         [preset setObject:@"160" forKey:@"AudioBitRate"];
3985         /* Subtitles*/
3986         [preset setObject:@"None" forKey:@"Subtitles"];
3987         
3988
3989     [preset autorelease];
3990     return preset;
3991
3992 }
3993
3994 - (NSDictionary *)createPSThreePreset
3995 {
3996     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3997         /* Get the New Preset Name from the field in the AddPresetPanel */
3998     [preset setObject:@"PS3" forKey:@"PresetName"];
3999         /*Set whether or not this is a user preset where 0 is factory, 1 is user*/
4000         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4001         /*Set whether or not this is default, at creation set to 0*/
4002         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4003         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4004         [preset setObject:[NSNumber numberWithInt:2] forKey:@"UsesPictureSettings"];
4005         /* Get the New Preset Description from the field in the AddPresetPanel */
4006     [preset setObject:@"HandBrake's settings for the Sony PlayStation 3." forKey:@"PresetDescription"];
4007         /* File Format */
4008     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
4009         /* Chapter Markers*/
4010          [preset setObject:[NSNumber numberWithInt:0] forKey:@"ChapterMarkers"];
4011         /* Codecs */
4012         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
4013         /* Video encoder */
4014         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4015         /* x264 Option String (We can use this to tweak the appleTV output)*/
4016         [preset setObject:@"level=41:subme=5:me=umh" forKey:@"x264Option"];
4017         /* Video quality */
4018         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4019         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4020         [preset setObject:@"2500" forKey:@"VideoAvgBitrate"];
4021         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4022         
4023         /* Video framerate */
4024         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4025         /* GrayScale */
4026         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4027         /* 2 Pass Encoding */
4028         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
4029         
4030         /*Picture Settings*/
4031         /* For PS3 we only want to retain UsesMaxPictureSettings
4032         which depend on the source dvd picture settings, so we don't
4033         record the current dvd's picture info since it will vary from
4034         source to source*/
4035         /* Use Max Picture settings for whatever the dvd is.*/
4036         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4037         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4038         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4039         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4040         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4041         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4042         /* Set crop settings here */
4043         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4044         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4045     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4046         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4047         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4048         
4049         /*Audio*/
4050         /* Audio Sample Rate*/
4051         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4052         /* Audio Bitrate Rate*/
4053         [preset setObject:@"160" forKey:@"AudioBitRate"];
4054         /* Subtitles*/
4055         [preset setObject:@"None" forKey:@"Subtitles"];
4056         
4057
4058     [preset autorelease];
4059     return preset;
4060
4061 }
4062 - (NSDictionary *)createPSPPreset
4063 {
4064     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4065         /* Get the New Preset Name from the field in the AddPresetPanel */
4066     [preset setObject:@"PSP" forKey:@"PresetName"];
4067         /*Set whether or not this is a user preset where 0 is factory, 1 is user*/
4068         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4069         /*Set whether or not this is default, at creation set to 0*/
4070         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4071         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4072         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4073         /* Get the New Preset Description from the field in the AddPresetPanel */
4074     [preset setObject:@"HandBrake's settings for the Sony PlayStation Portable." forKey:@"PresetDescription"];
4075         /* File Format */
4076     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
4077         /* Chapter Markers*/
4078          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4079         /* Codecs */
4080         [preset setObject:@"MPEG-4 Video / AAC Audio" forKey:@"FileCodecs"];
4081         /* Video encoder */
4082         [preset setObject:@"FFmpeg" forKey:@"VideoEncoder"];
4083         /* x264 Option String (We can use this to tweak the appleTV output)*/
4084         [preset setObject:@"" forKey:@"x264Option"];
4085         /* Video quality */
4086         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4087         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4088         [preset setObject:@"1024" forKey:@"VideoAvgBitrate"];
4089         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4090         
4091         /* Video framerate */
4092         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4093         /* GrayScale */
4094         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4095         /* 2 Pass Encoding */
4096         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
4097         
4098         /*Picture Settings*/
4099         /* For PS3 we only want to retain UsesMaxPictureSettings
4100         which depend on the source dvd picture settings, so we don't
4101         record the current dvd's picture info since it will vary from
4102         source to source*/
4103         /* Use Max Picture settings for whatever the dvd is.*/
4104         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
4105         [preset setObject:@"368" forKey:@"PictureWidth"];
4106         [preset setObject:@"208" forKey:@"PictureHeight"];
4107         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
4108         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4109         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
4110         /* Set crop settings here */
4111         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4112         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4113         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4114     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4115         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4116         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4117         
4118         /*Audio*/
4119         /* Audio Sample Rate*/
4120         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4121         /* Audio Bitrate Rate*/
4122         [preset setObject:@"128" forKey:@"AudioBitRate"];
4123         /* Subtitles*/
4124         [preset setObject:@"None" forKey:@"Subtitles"];
4125         
4126
4127     [preset autorelease];
4128     return preset;
4129
4130 }
4131
4132 - (NSDictionary *)createNormalPreset
4133 {
4134     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4135         /* Get the New Preset Name from the field in the AddPresetPanel */
4136     [preset setObject:@"Normal" forKey:@"PresetName"];
4137         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4138         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4139         /*Set whether or not this is default, at creation set to 0*/
4140         [preset setObject:[NSNumber numberWithInt:1] forKey:@"Default"];
4141         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4142         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4143         /* Get the New Preset Description from the field in the AddPresetPanel */
4144     [preset setObject:@"HandBrake's normal, default settings." forKey:@"PresetDescription"];
4145         /* File Format */
4146     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
4147         /* Chapter Markers*/
4148          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4149     /* Codecs */
4150         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
4151         /* Video encoder */
4152         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4153         /* x264 Option String */
4154         [preset setObject:@"ref=2:bframes=2:subme=5:me=umh" forKey:@"x264Option"];
4155         /* Video quality */
4156         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4157         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4158         [preset setObject:@"1500" forKey:@"VideoAvgBitrate"];
4159         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4160         
4161         /* Video framerate */
4162         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4163         /* GrayScale */
4164         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4165         /* 2 Pass Encoding */
4166         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
4167         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
4168         
4169         /*Picture Settings*/
4170         //hb_job_t * job = fTitle->job;
4171         /* Basic Picture Settings */
4172         /* Use Max Picture settings for whatever the dvd is.*/
4173         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4174         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4175         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4176         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4177         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4178         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4179         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4180         /* Set crop settings here */
4181         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4182         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4183     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4184         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4185         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4186         
4187         /*Audio*/
4188         /* Audio Sample Rate*/
4189         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4190         /* Audio Bitrate Rate*/
4191         [preset setObject:@"160" forKey:@"AudioBitRate"];
4192         /* Subtitles*/
4193         [preset setObject:@"None" forKey:@"Subtitles"];
4194         
4195
4196     [preset autorelease];
4197     return preset;
4198
4199 }
4200
4201 - (NSDictionary *)createClassicPreset
4202 {
4203     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4204         /* Get the New Preset Name from the field in the AddPresetPanel */
4205     [preset setObject:@"Classic" forKey:@"PresetName"];
4206         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4207         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4208         /*Set whether or not this is default, at creation set to 0*/
4209         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4210         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4211         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4212         /* Get the New Preset Description from the field in the AddPresetPanel */
4213     [preset setObject:@"HandBrake's traditional, faster, lower-quality settings." forKey:@"PresetDescription"];
4214         /* File Format */
4215     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
4216         /* Chapter Markers*/
4217          [preset setObject:[NSNumber numberWithInt:0] forKey:@"ChapterMarkers"];
4218     /* Codecs */
4219         [preset setObject:@"MPEG-4 Video / AAC Audio" forKey:@"FileCodecs"];
4220         /* Video encoder */
4221         [preset setObject:@"FFmpeg" forKey:@"VideoEncoder"];
4222         /* x264 Option String */
4223         [preset setObject:@"" forKey:@"x264Option"];
4224         /* Video quality */
4225         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4226         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4227         [preset setObject:@"1000" forKey:@"VideoAvgBitrate"];
4228         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4229         
4230         /* Video framerate */
4231         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4232         /* GrayScale */
4233         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4234         /* 2 Pass Encoding */
4235         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
4236         
4237         /*Picture Settings*/
4238         //hb_job_t * job = fTitle->job;
4239         /* Basic Picture Settings */
4240         /* Use Max Picture settings for whatever the dvd is.*/
4241         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4242         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4243         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4244         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4245         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
4246         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4247         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
4248         /* Set crop settings here */
4249         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4250         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4251     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4252         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4253         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4254         
4255         /*Audio*/
4256         /* Audio Sample Rate*/
4257         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4258         /* Audio Bitrate Rate*/
4259         [preset setObject:@"160" forKey:@"AudioBitRate"];
4260         /* Subtitles*/
4261         [preset setObject:@"None" forKey:@"Subtitles"];
4262         
4263
4264     [preset autorelease];
4265     return preset;
4266
4267 }
4268
4269 - (NSDictionary *)createFilmPreset
4270 {
4271     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4272         /* Get the New Preset Name from the field in the AddPresetPanel */
4273     [preset setObject:@"Film" forKey:@"PresetName"];
4274         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4275         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4276         /*Set whether or not this is default, at creation set to 0*/
4277         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4278         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4279         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4280         /* Get the New Preset Description from the field in the AddPresetPanel */
4281     [preset setObject:@"HandBrake's preset for feature films." forKey:@"PresetDescription"];
4282         /* File Format */
4283     [preset setObject:@"MKV file" forKey:@"FileFormat"];
4284         /* Chapter Markers*/
4285          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4286     /* Codecs */
4287         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
4288         /* Video encoder */
4289         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4290         /* x264 Option String */
4291         [preset setObject:@"ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:no-fast-pskip" forKey:@"x264Option"];
4292         /* Video quality */
4293         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4294         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4295         [preset setObject:@"1800" forKey:@"VideoAvgBitrate"];
4296         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4297         
4298         /* Video framerate */
4299         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4300         /* GrayScale */
4301         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4302         /* 2 Pass Encoding */
4303         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
4304         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
4305         
4306         /*Picture Settings*/
4307         //hb_job_t * job = fTitle->job;
4308         /* Basic Picture Settings */
4309         /* Use Max Picture settings for whatever the dvd is.*/
4310         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4311         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4312         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4313         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4314         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4315         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4316         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4317         /* Set crop settings here */
4318         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4319         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4320     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4321         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4322         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4323         
4324         /*Audio*/
4325         /* Audio Sample Rate*/
4326         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4327         /* Audio Bitrate Rate*/
4328         [preset setObject:@"160" forKey:@"AudioBitRate"];
4329         /* Subtitles*/
4330         [preset setObject:@"None" forKey:@"Subtitles"];
4331         
4332
4333     [preset autorelease];
4334     return preset;
4335
4336 }
4337
4338 - (NSDictionary *)createTelevisionPreset
4339 {
4340     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4341         /* Get the New Preset Name from the field in the AddPresetPanel */
4342     [preset setObject:@"Television" forKey:@"PresetName"];
4343         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4344         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4345         /*Set whether or not this is default, at creation set to 0*/
4346         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4347         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4348         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4349         /* Get the New Preset Description from the field in the AddPresetPanel */
4350     [preset setObject:@"HandBrake's settings for video from television." forKey:@"PresetDescription"];
4351         /* File Format */
4352     [preset setObject:@"MKV file" forKey:@"FileFormat"];
4353         /* Chapter Markers*/
4354          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4355     /* Codecs */
4356         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
4357         /* Video encoder */
4358         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4359         /* x264 Option String */
4360         [preset setObject:@"ref=3:mixed-refs:bframes=16:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip" forKey:@"x264Option"];
4361         /* Video quality */
4362         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4363         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4364         [preset setObject:@"1300" forKey:@"VideoAvgBitrate"];
4365         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4366         
4367         /* Video framerate */
4368         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4369         /* GrayScale */
4370         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4371         /* 2 Pass Encoding */
4372         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
4373         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
4374         
4375         /*Picture Settings*/
4376         //hb_job_t * job = fTitle->job;
4377         /* Basic Picture Settings */
4378         /* Use Max Picture settings for whatever the dvd is.*/
4379         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4380         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4381         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4382         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4383         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
4384         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureDeinterlace"];
4385         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
4386         /* Set crop settings here */
4387         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4388         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4389     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4390         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4391         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4392         
4393         /*Audio*/
4394         /* Audio Sample Rate*/
4395         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4396         /* Audio Bitrate Rate*/
4397         [preset setObject:@"160" forKey:@"AudioBitRate"];
4398         /* Subtitles*/
4399         [preset setObject:@"None" forKey:@"Subtitles"];
4400         
4401
4402     [preset autorelease];
4403     return preset;
4404
4405 }
4406
4407 - (NSDictionary *)createAnimationPreset
4408 {
4409     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4410         /* Get the New Preset Name from the field in the AddPresetPanel */
4411     [preset setObject:@"Animation" forKey:@"PresetName"];
4412         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4413         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4414         /*Set whether or not this is default, at creation set to 0*/
4415         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4416         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4417         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4418         /* Get the New Preset Description from the field in the AddPresetPanel */
4419     [preset setObject:@"HandBrake's settings for cartoons, anime, and CGI." forKey:@"PresetDescription"];
4420         /* File Format */
4421     [preset setObject:@"MKV file" forKey:@"FileFormat"];
4422         /* Chapter Markers*/
4423          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4424     /* Codecs */
4425         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
4426         /* Video encoder */
4427         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4428         /* x264 Option String */
4429         [preset setObject:@"ref=5:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2" forKey:@"x264Option"];
4430         /* Video quality */
4431         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4432         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4433         [preset setObject:@"1000" forKey:@"VideoAvgBitrate"];
4434         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4435         
4436         /* Video framerate */
4437         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4438         /* GrayScale */
4439         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4440         /* 2 Pass Encoding */
4441         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
4442         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
4443         
4444         /*Picture Settings*/
4445         //hb_job_t * job = fTitle->job;
4446         /* Basic Picture Settings */
4447         /* Use Max Picture settings for whatever the dvd is.*/
4448         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4449         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4450         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4451         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4452         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4453         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureDeinterlace"];
4454         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4455         /* Set crop settings here */
4456         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4457         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4458     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4459         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4460         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4461         
4462         /*Audio*/
4463         /* Audio Sample Rate*/
4464         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4465         /* Audio Bitrate Rate*/
4466         [preset setObject:@"160" forKey:@"AudioBitRate"];
4467         /* Subtitles*/
4468         [preset setObject:@"None" forKey:@"Subtitles"];
4469         
4470
4471     [preset autorelease];
4472     return preset;
4473
4474 }
4475
4476 - (NSDictionary *)createQuickTimePreset
4477 {
4478     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4479         /* Get the New Preset Name from the field in the AddPresetPanel */
4480     [preset setObject:@"QuickTime" forKey:@"PresetName"];
4481         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4482         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4483         /*Set whether or not this is default, at creation set to 0*/
4484         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4485         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4486         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4487         /* Get the New Preset Description from the field in the AddPresetPanel */
4488     [preset setObject:@"HandBrake's high quality settings for use with QuickTime. It can be slow, so use it when the Normal preset doesn't look good enough." forKey:@"PresetDescription"];
4489         /* File Format */
4490     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
4491         /* Chapter Markers*/
4492          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4493     /* Codecs */
4494         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
4495         /* Video encoder */
4496         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4497         /* x264 Option String */
4498         [preset setObject:@"ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:trellis=1:no-fast-pskip" forKey:@"x264Option"];
4499         /* Video quality */
4500         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4501         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4502         [preset setObject:@"2000" forKey:@"VideoAvgBitrate"];
4503         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4504         
4505         /* Video framerate */
4506         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4507         /* GrayScale */
4508         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4509         /* 2 Pass Encoding */
4510         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
4511         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
4512         
4513         /*Picture Settings*/
4514         //hb_job_t * job = fTitle->job;
4515         /* Basic Picture Settings */
4516         /* Use Max Picture settings for whatever the dvd is.*/
4517         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4518         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4519         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4520         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4521         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4522         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4523         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4524         /* Set crop settings here */
4525         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4526         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4527     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4528         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4529         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4530         
4531         /*Audio*/
4532         /* Audio Sample Rate*/
4533         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4534         /* Audio Bitrate Rate*/
4535         [preset setObject:@"160" forKey:@"AudioBitRate"];
4536         /* Subtitles*/
4537         [preset setObject:@"None" forKey:@"Subtitles"];
4538         
4539
4540     [preset autorelease];
4541     return preset;
4542
4543 }
4544
4545 - (NSDictionary *)createBedlamPreset
4546 {
4547     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4548         /* Get the New Preset Name from the field in the AddPresetPanel */
4549     [preset setObject:@"Bedlam" forKey:@"PresetName"];
4550         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4551         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4552         /*Set whether or not this is default, at creation set to 0*/
4553         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4554         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4555         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4556         /* Get the New Preset Description from the field in the AddPresetPanel */
4557     [preset setObject:@"HandBrake's settings maxed out for slowest encoding and highest quality. Use at your own risk. So slow it's not just insane...it's a trip to the looney bin." forKey:@"PresetDescription"];
4558         /* File Format */
4559     [preset setObject:@"MKV file" forKey:@"FileFormat"];
4560         /* Chapter Markers*/
4561          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4562     /* Codecs */
4563         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
4564         /* Video encoder */
4565         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4566         /* x264 Option String */
4567         [preset setObject:@"ref=16:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:me-range=64:analyse=all:8x8dct:trellis=2:no-fast-pskip:no-dct-decimate:filter=-2,-1" forKey:@"x264Option"];
4568         /* Video quality */
4569         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4570         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4571         [preset setObject:@"1800" forKey:@"VideoAvgBitrate"];
4572         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4573         
4574         /* Video framerate */
4575         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4576         /* GrayScale */
4577         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4578         /* 2 Pass Encoding */
4579         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
4580         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
4581         
4582         /*Picture Settings*/
4583         //hb_job_t * job = fTitle->job;
4584         /* Basic Picture Settings */
4585         /* Use Max Picture settings for whatever the dvd is.*/
4586         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4587         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4588         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4589         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4590         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4591         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4592         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4593         /* Set crop settings here */
4594         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4595         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4596     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4597         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4598         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4599         
4600         /*Audio*/
4601         /* Audio Sample Rate*/
4602         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4603         /* Audio Bitrate Rate*/
4604         [preset setObject:@"160" forKey:@"AudioBitRate"];
4605         /* Subtitles*/
4606         [preset setObject:@"None" forKey:@"Subtitles"];
4607         
4608
4609     [preset autorelease];
4610     return preset;
4611
4612 }
4613
4614 - (NSDictionary *)createiPhonePreset
4615 {
4616     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4617         /* Get the New Preset Name from the field in the AddPresetPanel */
4618     [preset setObject:@"iPhone / iPod Touch" forKey:@"PresetName"];
4619         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4620         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4621         /*Set whether or not this is default, at creation set to 0*/
4622         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4623         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4624         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4625         /* Get the New Preset Description from the field in the AddPresetPanel */
4626     [preset setObject:@"HandBrake's settings for the iPhone." forKey:@"PresetDescription"];
4627         /* File Format */
4628     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
4629         /* Chapter Markers*/
4630          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4631     /* Codecs */
4632         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
4633         /* Video encoder */
4634         [preset setObject:@"x264 (h.264 iPod)" forKey:@"VideoEncoder"];
4635         /* x264 Option String */
4636         [preset setObject:@"cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1" forKey:@"x264Option"];
4637         /* Video quality */
4638         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4639         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4640         [preset setObject:@"960" forKey:@"VideoAvgBitrate"];
4641         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4642         
4643         /* Video framerate */
4644         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4645         /* GrayScale */
4646         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4647         /* 2 Pass Encoding */
4648         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
4649         
4650         /*Picture Settings*/
4651         //hb_job_t * job = fTitle->job;
4652         /* Basic Picture Settings */
4653         /* Use Max Picture settings for whatever the dvd is.*/
4654         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
4655         [preset setObject:[NSNumber numberWithInt:480] forKey:@"PictureWidth"];
4656         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4657         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
4658         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4659         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
4660         /* Set crop settings here */
4661         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4662         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4663         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4664     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4665         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4666         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4667         
4668         /*Audio*/
4669         /* Audio Sample Rate*/
4670         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4671         /* Audio Bitrate Rate*/
4672         [preset setObject:@"128" forKey:@"AudioBitRate"];
4673         /* Subtitles*/
4674         [preset setObject:@"None" forKey:@"Subtitles"];
4675         
4676
4677     [preset autorelease];
4678     return preset;
4679
4680 }
4681
4682 - (NSDictionary *)createDeuxSixQuatrePreset
4683 {
4684     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4685         /* Get the New Preset Name from the field in the AddPresetPanel */
4686     [preset setObject:@"Deux Six Quatre" forKey:@"PresetName"];
4687         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4688         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4689         /*Set whether or not this is default, at creation set to 0*/
4690         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4691         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4692         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4693         /* Get the New Preset Description from the field in the AddPresetPanel */
4694     [preset setObject:@"HandBrake's preset for true high profile x264 quality. A good balance of quality and speed, based on community standards found in the wild. This preset will give you a much better sense of x264's capabilities than vanilla main profile." forKey:@"PresetDescription"];
4695         /* File Format */
4696     [preset setObject:@"MKV file" forKey:@"FileFormat"];
4697         /* Chapter Markers*/
4698          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4699     /* Codecs */
4700         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
4701         /* Video encoder */
4702         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4703         /* x264 Option String */
4704         [preset setObject:@"ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip" forKey:@"x264Option"];
4705         /* Video quality */
4706         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4707         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4708         [preset setObject:@"1600" forKey:@"VideoAvgBitrate"];
4709         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4710         
4711         /* Video framerate */
4712         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4713         /* GrayScale */
4714         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4715         /* 2 Pass Encoding */
4716         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
4717         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
4718         
4719         /*Picture Settings*/
4720         //hb_job_t * job = fTitle->job;
4721         /* Basic Picture Settings */
4722         /* Use Max Picture settings for whatever the dvd is.*/
4723         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4724         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4725         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4726         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4727         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4728         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4729         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4730         /* Set crop settings here */
4731         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4732         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4733     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4734         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4735         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4736         
4737         /*Audio*/
4738         /* Audio Sample Rate*/
4739         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4740         /* Audio Bitrate Rate*/
4741         [preset setObject:@"160" forKey:@"AudioBitRate"];
4742         /* Subtitles*/
4743         [preset setObject:@"None" forKey:@"Subtitles"];
4744         
4745
4746     [preset autorelease];
4747     return preset;
4748
4749 }
4750
4751 - (NSDictionary *)createBrokePreset
4752 {
4753     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4754         /* Get the New Preset Name from the field in the AddPresetPanel */
4755     [preset setObject:@"Broke" forKey:@"PresetName"];
4756         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4757         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4758         /*Set whether or not this is default, at creation set to 0*/
4759         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4760         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4761         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4762         /* Get the New Preset Description from the field in the AddPresetPanel */
4763     [preset setObject:@"HandBrake's preset for people without a lot of money to waste on hard drives. Tries to maximize quality for burning to CDs, so you can party like it's 1999." forKey:@"PresetDescription"];
4764         /* File Format */
4765     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
4766         /* Chapter Markers*/
4767          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4768     /* Codecs */
4769         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
4770         /* Video encoder */
4771         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4772         /* x264 Option String */
4773         [preset setObject:@"ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip" forKey:@"x264Option"];
4774         /* Video quality */
4775         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoQualityType"];
4776         [preset setObject:@"695" forKey:@"VideoTargetSize"];
4777         [preset setObject:@"1600" forKey:@"VideoAvgBitrate"];
4778         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4779         
4780         /* Video framerate */
4781         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4782         /* GrayScale */
4783         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4784         /* 2 Pass Encoding */
4785         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
4786         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
4787         
4788         /*Picture Settings*/
4789         //hb_job_t * job = fTitle->job;
4790         /* Basic Picture Settings */
4791         /* Use Max Picture settings for whatever the dvd is.*/
4792         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
4793         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4794         [preset setObject:[NSNumber numberWithInt:640] forKey:@"PictureWidth"];
4795         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4796         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
4797         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4798         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
4799         /* Set crop settings here */
4800         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4801         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4802     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4803         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4804         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4805         
4806         /*Audio*/
4807         /* Audio Sample Rate*/
4808         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4809         /* Audio Bitrate Rate*/
4810         [preset setObject:@"128" forKey:@"AudioBitRate"];
4811         /* Subtitles*/
4812         [preset setObject:@"None" forKey:@"Subtitles"];
4813         
4814
4815     [preset autorelease];
4816     return preset;
4817
4818 }
4819
4820 - (NSDictionary *)createBlindPreset
4821 {
4822     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4823         /* Get the New Preset Name from the field in the AddPresetPanel */
4824     [preset setObject:@"Blind" forKey:@"PresetName"];
4825         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4826         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4827         /*Set whether or not this is default, at creation set to 0*/
4828         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4829         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4830         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4831         /* Get the New Preset Description from the field in the AddPresetPanel */
4832     [preset setObject:@"HandBrake's preset for impatient people who don't care about picture quality." forKey:@"PresetDescription"];
4833         /* File Format */
4834     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
4835         /* Chapter Markers*/
4836          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4837     /* Codecs */
4838         [preset setObject:@"MPEG-4 Video / AAC Audio" forKey:@"FileCodecs"];
4839         /* Video encoder */
4840         [preset setObject:@"FFmpeg" forKey:@"VideoEncoder"];
4841         /* x264 Option String */
4842         [preset setObject:@"" forKey:@"x264Option"];
4843         /* Video quality */
4844         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4845         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4846         [preset setObject:@"512" forKey:@"VideoAvgBitrate"];
4847         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4848         
4849         /* Video framerate */
4850         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4851         /* GrayScale */
4852         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4853         /* 2 Pass Encoding */
4854         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
4855         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTurboTwoPass"];
4856         
4857         /*Picture Settings*/
4858         //hb_job_t * job = fTitle->job;
4859         /* Basic Picture Settings */
4860         /* Use Max Picture settings for whatever the dvd is.*/
4861         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
4862         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4863         [preset setObject:[NSNumber numberWithInt:512] forKey:@"PictureWidth"];
4864         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4865         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
4866         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4867         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
4868         /* Set crop settings here */
4869         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4870         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4871     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4872         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4873         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4874         
4875         /*Audio*/
4876         /* Audio Sample Rate*/
4877         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4878         /* Audio Bitrate Rate*/
4879         [preset setObject:@"128" forKey:@"AudioBitRate"];
4880         /* Subtitles*/
4881         [preset setObject:@"None" forKey:@"Subtitles"];
4882         
4883
4884     [preset autorelease];
4885     return preset;
4886
4887 }
4888
4889 - (NSDictionary *)createCRFPreset
4890 {
4891     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4892         /* Get the New Preset Name from the field in the AddPresetPanel */
4893     [preset setObject:@"Constant Quality Rate" forKey:@"PresetName"];
4894         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4895         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4896         /*Set whether or not this is default, at creation set to 0*/
4897         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4898         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4899         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4900         /* Get the New Preset Description from the field in the AddPresetPanel */
4901     [preset setObject:@"HandBrake's preset for consistently excellent quality in one pass, with the downside of entirely unpredictable file sizes and bitrates." forKey:@"PresetDescription"];
4902         /* File Format */
4903     [preset setObject:@"MKV file" forKey:@"FileFormat"];
4904         /* Chapter Markers*/
4905          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4906     /* Codecs */
4907         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
4908         /* Video encoder */
4909         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4910         /* x264 Option String */
4911         [preset setObject:@"ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh" forKey:@"x264Option"];
4912         /* Video quality */
4913         [preset setObject:[NSNumber numberWithInt:2] forKey:@"VideoQualityType"];
4914         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4915         [preset setObject:@"2000" forKey:@"VideoAvgBitrate"];
4916         [preset setObject:[NSNumber numberWithFloat:0.6471] forKey:@"VideoQualitySlider"];
4917         
4918         /* Video framerate */
4919         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4920         /* GrayScale */
4921         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4922         /* 2 Pass Encoding */
4923         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
4924         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTurboTwoPass"];
4925         
4926         /*Picture Settings*/
4927         //hb_job_t * job = fTitle->job;
4928         /* Basic Picture Settings */
4929         /* Use Max Picture settings for whatever the dvd is.*/
4930         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4931         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4932         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4933         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4934         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4935         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4936         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4937         /* Set crop settings here */
4938         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4939         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4940     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4941         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4942         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4943         
4944         /*Audio*/
4945         /* Audio Sample Rate*/
4946         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4947         /* Audio Bitrate Rate*/
4948         [preset setObject:@"160" forKey:@"AudioBitRate"];
4949         /* Subtitles*/
4950         [preset setObject:@"None" forKey:@"Subtitles"];
4951         
4952
4953     [preset autorelease];
4954     return preset;
4955
4956 }
4957
4958
4959
4960
4961
4962 @end