OSDN Git Service

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