OSDN Git Service

MacGui:Oops. WTF was I hiding direct prediction in the advanced options based on...
[handbrake-jp/handbrake-jp-git.git] / macosx / HBAdvancedController.m
1 /* HBAdvancedController
2
3     This file is part of the HandBrake source code.
4     Homepage: <http://handbrake.fr/>.
5     It may be used under the terms of the GNU General Public License. */
6     
7 #import "HBAdvancedController.h"
8
9 @implementation HBAdvancedController
10
11 - (id)init
12 {
13     [super init];
14     [self loadMyNibFile];
15     
16     return self;
17 }
18
19 - (void) setView: (NSBox *) box
20 {
21     fOptionsBox = box;
22     [fOptionsBox setContentView:fX264optView];
23 }
24
25 - (BOOL) loadMyNibFile
26 {
27     if(![NSBundle loadNibNamed:@"AdvancedView" owner:self])
28     {
29         NSLog(@"Warning! Could not load myNib file.\n");
30         return NO;
31     }
32     
33     return YES;
34 }
35
36 - (NSString *) optionsString
37 {
38     return [fDisplayX264Options stringValue];
39 }
40
41 - (void) setOptions: (NSString *)string
42 {
43     [fDisplayX264Options setStringValue:string];
44     [self X264AdvancedOptionsSet:nil];
45 }
46
47 - (void) setHidden: (BOOL) hide
48 {
49     if(hide)
50     {
51         [fOptionsBox setContentView:fEmptyView];
52         [fX264optViewTitleLabel setStringValue: @"Only Used With The x264 (H.264) Codec"];
53     }
54     else
55     {
56         [fOptionsBox setContentView:fX264optView];
57         [fX264optViewTitleLabel setStringValue: @""];
58     }
59     return;
60 }
61
62  - (void) enableUI: (bool) b
63 {
64     unsigned i;
65     NSControl * controls[] =
66       { fX264optViewTitleLabel,fDisplayX264Options,fDisplayX264OptionsLabel,fX264optBframesLabel,
67         fX264optBframesPopUp,fX264optRefLabel,fX264optRefPopUp,fX264optNfpskipLabel,fX264optNfpskipSwitch,
68         fX264optNodctdcmtLabel,fX264optNodctdcmtSwitch,fX264optSubmeLabel,fX264optSubmePopUp,
69         fX264optTrellisLabel,fX264optTrellisPopUp,fX264optMixedRefsLabel,fX264optMixedRefsSwitch,
70         fX264optMotionEstLabel,fX264optMotionEstPopUp,fX264optMERangeLabel,fX264optMERangePopUp,
71         fX264optWeightBLabel,fX264optWeightBSwitch,fX264optBRDOLabel,fX264optBRDOSwitch,
72         fX264optBPyramidLabel,fX264optBPyramidSwitch,fX264optBiMELabel,fX264optBiMESwitch,
73         fX264optDirectPredLabel,fX264optDirectPredPopUp,fX264optDeblockLabel,fX264optAnalyseLabel,
74         fX264optAnalysePopUp,fX264opt8x8dctLabel,fX264opt8x8dctSwitch,fX264optCabacLabel,fX264optCabacSwitch,
75         fX264optAlphaDeblockPopUp,fX264optBetaDeblockPopUp};
76
77     for( i = 0; i < sizeof( controls ) / sizeof( NSControl * ); i++ )
78     {
79         if( [[controls[i] className] isEqualToString: @"NSTextField"] )
80         {
81             NSTextField * tf = (NSTextField *) controls[i];
82             if( ![tf isBezeled] )
83             {
84                 [tf setTextColor: b ? [NSColor controlTextColor] :
85                     [NSColor disabledControlTextColor]];
86                 continue;
87             }
88         }
89         [controls[i] setEnabled: b];
90
91     }
92     
93     [fX264optView setWantsLayer:YES];
94 }
95
96 - (void)dealloc
97 {
98     [super dealloc];
99 }
100
101 /**
102  * Populates the option widgets
103  */
104 - (IBAction) X264AdvancedOptionsSet: (id) sender
105 {
106     /*Set opt widget values here*/
107     
108     /*B-Frames fX264optBframesPopUp*/
109     int i;
110     [fX264optBframesPopUp removeAllItems];
111     [fX264optBframesPopUp addItemWithTitle:@"Default (0)"];
112     for (i=0; i<17;i++)
113     {
114         [fX264optBframesPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
115     }
116     
117     /*Reference Frames fX264optRefPopUp*/
118     [fX264optRefPopUp removeAllItems];
119     [fX264optRefPopUp addItemWithTitle:@"Default (1)"];
120     for (i=0; i<17;i++)
121     {
122         [fX264optRefPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
123     }
124     
125     /*No Fast P-Skip fX264optNfpskipSwitch BOOLEAN*/
126     [fX264optNfpskipSwitch setState:0];
127     
128     /*No Dict Decimate fX264optNodctdcmtSwitch BOOLEAN*/
129     [fX264optNodctdcmtSwitch setState:0];    
130     
131     /*Sub Me fX264optSubmePopUp*/
132     [fX264optSubmePopUp removeAllItems];
133     [fX264optSubmePopUp addItemWithTitle:@"Default (6)"];
134     for (i=0; i<8;i++)
135     {
136         [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
137     }
138     
139     /*Trellis fX264optTrellisPopUp*/
140     [fX264optTrellisPopUp removeAllItems];
141     [fX264optTrellisPopUp addItemWithTitle:@"Default (0)"];
142     for (i=0; i<3;i++)
143     {
144         [fX264optTrellisPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
145     }
146     [fX264optTrellisPopUp setWantsLayer:YES];
147     
148     /*Mixed-references fX264optMixedRefsSwitch BOOLEAN*/
149     [fX264optMixedRefsSwitch setState:0];
150     [fX264optMixedRefsSwitch setWantsLayer:YES];
151     
152     /*Motion Estimation fX264optMotionEstPopUp*/
153     [fX264optMotionEstPopUp removeAllItems];
154     [fX264optMotionEstPopUp addItemWithTitle:@"Default (Hexagon)"];
155     [fX264optMotionEstPopUp addItemWithTitle:@"Diamond"];
156     [fX264optMotionEstPopUp addItemWithTitle:@"Hexagon"];
157     [fX264optMotionEstPopUp addItemWithTitle:@"Uneven Multi-Hexagon"];
158     [fX264optMotionEstPopUp addItemWithTitle:@"Exhaustive"];
159     
160     /*Motion Estimation range fX264optMERangePopUp*/
161     [fX264optMERangePopUp removeAllItems];
162     [fX264optMERangePopUp addItemWithTitle:@"Default (16)"];
163     for (i=4; i<65;i++)
164     {
165         [fX264optMERangePopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
166     }
167     
168     /*Weighted B-Frame Prediction fX264optWeightBSwitch BOOLEAN*/
169     [fX264optWeightBSwitch setState:0];
170     [fX264optWeightBSwitch setWantsLayer:YES];
171     
172     /*B-Frame Rate Distortion Optimization fX264optBRDOSwitch BOOLEAN*/
173     [fX264optBRDOSwitch setState:0];
174     [fX264optBRDOSwitch setWantsLayer:YES];
175     
176     /*B-frame Pyramids fX264optBPyramidSwitch BOOLEAN*/
177     [fX264optBPyramidSwitch setState:0];
178     [fX264optBPyramidSwitch setWantsLayer:YES];
179     
180     /*Bidirectional Motion Estimation Refinement fX264optBiMESwitch BOOLEAN*/
181     [fX264optBiMESwitch setState:0];
182     [fX264optBiMESwitch setWantsLayer:YES];
183     
184     /*Direct B-Frame Prediction Mode fX264optDirectPredPopUp*/
185     [fX264optDirectPredPopUp removeAllItems];
186     [fX264optDirectPredPopUp addItemWithTitle:@"Default (Spatial)"];
187     [fX264optDirectPredPopUp addItemWithTitle:@"None"];
188     [fX264optDirectPredPopUp addItemWithTitle:@"Spatial"];
189     [fX264optDirectPredPopUp addItemWithTitle:@"Temporal"];
190     [fX264optDirectPredPopUp addItemWithTitle:@"Automatic"];
191     [fX264optDirectPredPopUp setWantsLayer:YES];
192     
193     /*Alpha Deblock*/
194     [fX264optAlphaDeblockPopUp removeAllItems];
195     [fX264optAlphaDeblockPopUp addItemWithTitle:@"Default (0)"];
196     for (i=-6; i<7;i++)
197     {
198         [fX264optAlphaDeblockPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
199     }
200
201     /*Beta Deblock*/
202     [fX264optBetaDeblockPopUp removeAllItems];
203     [fX264optBetaDeblockPopUp addItemWithTitle:@"Default (0)"];
204     for (i=-6; i<7;i++)
205     {
206         [fX264optBetaDeblockPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
207     }
208
209     /* Analysis fX264optAnalysePopUp */
210     [fX264optAnalysePopUp removeAllItems];
211     [fX264optAnalysePopUp addItemWithTitle:@"Default (some)"]; /* 0=default */
212     [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"None"]]; /* 1=none */
213     [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"All"]]; /* 2=all */
214
215     /* 8x8 DCT fX264op8x8dctSwitch */
216     [fX264opt8x8dctSwitch setState:0];
217     [fX264opt8x8dctSwitch setWantsLayer:YES];
218
219     /* CABAC fX264opCabacSwitch */
220     [fX264optCabacSwitch setState:1];
221
222     /* Standardize the option string */
223     [self X264AdvancedOptionsStandardizeOptString:nil];
224
225     /* Set Current GUI Settings based on newly standardized string */
226     [self X264AdvancedOptionsSetCurrentSettings:nil];
227
228     /* Fade out options that don't apply */
229     [self X264AdvancedOptionsAnimate: sender];
230 }
231
232 /**
233  * Cleans the option string to use a standard format of option=value
234  */
235 - (IBAction) X264AdvancedOptionsStandardizeOptString: (id) sender
236 {
237     /* Set widgets depending on the opt string in field */
238     NSString * thisOpt; // The separated option such as "bframes=3"
239     NSString * optName = @""; // The option name such as "bframes"
240     NSString * optValue = @"";// The option value such as "3"
241     NSString * changedOptString = @"";
242     NSArray *currentOptsArray;
243     
244     /*First, we get an opt string to process */
245     NSString *currentOptString = [fDisplayX264Options stringValue];
246     
247     /* Verify there is an opt string to process by making sure an
248        option is getting its value set. If so, start to process it. */
249     NSRange currentOptRange = [currentOptString rangeOfString:@"="];
250     if (currentOptRange.location != NSNotFound)
251     {
252         /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/
253         currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
254         
255         /*iterate through the array and get <opts> and <values*/
256         int loopcounter;
257         int currentOptsArrayCount = [currentOptsArray count];
258         for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
259         {
260             thisOpt = [currentOptsArray objectAtIndex:loopcounter];
261             
262             NSRange splitOptRange = [thisOpt rangeOfString:@"="];
263             if (splitOptRange.location != NSNotFound)
264             {
265                 optName = [thisOpt substringToIndex:splitOptRange.location];
266                 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
267                 
268                 /* Standardize the names here depending on whats in the string */
269                 optName = [self X264AdvancedOptionsStandardizeOptNames:optName];
270                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,optValue];    
271             }
272             else // No value given so we use a default of "1"
273             {
274                 optName = thisOpt;
275
276                 /* Standardize the names here depending on whats in the string */
277                 optName = [self X264AdvancedOptionsStandardizeOptNames:optName];
278                 thisOpt = [NSString stringWithFormat:@"%@=%d",optName,1];
279             }
280             
281             /* Construct New String for opts here.*/
282             if ([thisOpt isEqualToString:@""])
283             {
284                 /* Blank option, just add it to the string. (Why?) */
285                 changedOptString = [NSString stringWithFormat:@"%@%@",changedOptString,thisOpt];
286             }
287             else
288             {
289                 if ([changedOptString isEqualToString:@""])
290                 {
291                     /* Blank string, output the current option. */
292                     changedOptString = [NSString stringWithFormat:@"%@",thisOpt];
293                 }
294                 else
295                 {
296                     /* Option exists and string exists, so append the option
297                        to the string with a semi-colon inbetween them.       */
298                     changedOptString = [NSString stringWithFormat:@"%@:%@",changedOptString,thisOpt];
299                 }
300             }
301         }
302     }
303     
304     /* Change the option string to reflect the new standardized option string */
305     [fDisplayX264Options setStringValue:[NSString stringWithFormat:changedOptString]];
306 }
307
308 /**
309  * Cleans the option string to use a standard set of option names, by conflating synonyms.
310  */
311 - (NSString *) X264AdvancedOptionsStandardizeOptNames:(NSString *) cleanOptNameString
312 {
313     /* Reference Frames */
314     if ([cleanOptNameString isEqualToString:@"ref"] || [cleanOptNameString isEqualToString:@"frameref"])
315     {
316         cleanOptNameString = @"ref";
317     }
318     
319     /*No Fast PSkip nofast_pskip*/
320     if ([cleanOptNameString isEqualToString:@"no-fast-pskip"] || [cleanOptNameString isEqualToString:@"no_fast_pskip"] || [cleanOptNameString isEqualToString:@"nofast_pskip"])
321     {
322         cleanOptNameString = @"no-fast-pskip";
323     }
324     
325     /*No Dict Decimate*/
326     if ([cleanOptNameString isEqualToString:@"no-dct-decimate"] || [cleanOptNameString isEqualToString:@"no_dct_decimate"] || [cleanOptNameString isEqualToString:@"nodct_decimate"])
327     {
328         cleanOptNameString = @"no-dct-decimate";
329     }
330     
331     /*Subme*/
332     if ([cleanOptNameString isEqualToString:@"subme"])
333     {
334         cleanOptNameString = @"subq";
335     }
336     
337     /*ME Range*/
338     if ([cleanOptNameString isEqualToString:@"me-range"] || [cleanOptNameString isEqualToString:@"me_range"])
339         cleanOptNameString = @"merange";
340     
341     /*WeightB*/
342     if ([cleanOptNameString isEqualToString:@"weight-b"] || [cleanOptNameString isEqualToString:@"weight_b"])
343     {
344         cleanOptNameString = @"weightb";
345     }
346     
347     /*BRDO*/
348     if ([cleanOptNameString isEqualToString:@"b-rdo"] || [cleanOptNameString isEqualToString:@"b_rdo"])
349     {
350         cleanOptNameString = @"brdo";
351     }
352     
353     /*B Pyramid*/
354     if ([cleanOptNameString isEqualToString:@"b_pyramid"])
355     {
356         cleanOptNameString = @"b-pyramid";
357     }
358     
359     /*Direct Prediction*/
360     if ([cleanOptNameString isEqualToString:@"direct-pred"] || [cleanOptNameString isEqualToString:@"direct_pred"])
361     {
362         cleanOptNameString = @"direct";
363     }
364     
365     /*Deblocking*/
366     if ([cleanOptNameString isEqualToString:@"filter"])
367     {
368         cleanOptNameString = @"deblock";
369     }
370     
371     /*Analysis*/
372     if ([cleanOptNameString isEqualToString:@"partitions"])
373     {
374         cleanOptNameString = @"analyse";
375     }
376     
377     return cleanOptNameString;    
378 }
379
380 /**
381  * Fades options in and out depending on whether they're available..
382  */
383 - (IBAction) X264AdvancedOptionsAnimate: (id) sender
384 {
385     /* Lots of situations to cover.
386        - B-frames (when 0 turn of b-frame specific stuff, when < 2 disable b-pyramid)
387        - CABAC (when 0 turn off trellis)
388        - subme  (if under 6 turn off brdo)
389        - analysis (if none, turn off 8x8dct)
390        - refs (under 2, disable mixed-refs)
391     */
392     
393     if ( [fX264optBframesPopUp indexOfSelectedItem ] < 2)
394     {
395         /* If the b-frame widget is at 0 or 1, the user has chosen
396            not to use b-frames at all. So disable the options
397            that can only be used when b-frames are enabled.        */
398         [[fX264optWeightBSwitch animator] setHidden:YES];
399         [[fX264optWeightBLabel animator] setHidden:YES];
400         if ( [fX264optWeightBSwitch state] == 1 && sender != fX264optWeightBSwitch && sender != fX264optBRDOSwitch && sender != fX264optBPyramidSwitch && sender != fX264optBiMESwitch && sender != fX264optDirectPredPopUp)
401             [fX264optWeightBSwitch performClick:self];
402         
403         [[fX264optBRDOSwitch animator] setHidden:YES];
404         [[fX264optBRDOLabel animator] setHidden:YES];
405         if ( [fX264optBRDOSwitch state] == 1 && sender != fX264optWeightBSwitch && sender != fX264optBRDOSwitch && sender != fX264optBPyramidSwitch && sender != fX264optBiMESwitch && sender != fX264optDirectPredPopUp)
406             [fX264optBRDOSwitch performClick:self];
407
408         [[fX264optBPyramidSwitch animator] setHidden:YES];
409         [[fX264optBPyramidLabel animator] setHidden:YES];
410         if ( [fX264optBPyramidSwitch state] == 1 && sender != fX264optWeightBSwitch && sender != fX264optBRDOSwitch && sender != fX264optBPyramidSwitch && sender != fX264optBiMESwitch && sender != fX264optDirectPredPopUp)
411             [fX264optBPyramidSwitch performClick:self];
412
413         [[fX264optBiMESwitch animator] setHidden:YES];
414         [[fX264optBiMELabel animator] setHidden:YES];
415         if ( [fX264optBiMESwitch state] == 1 && sender != fX264optWeightBSwitch && sender != fX264optBRDOSwitch && sender != fX264optBPyramidSwitch && sender != fX264optBiMESwitch && sender != fX264optDirectPredPopUp)
416             [fX264optBiMESwitch performClick:self];
417
418         [[fX264optDirectPredPopUp animator] setHidden:YES];
419         [[fX264optDirectPredLabel animator] setHidden:YES];
420         if ( [fX264optDirectPredPopUp indexOfSelectedItem] > 0 && sender != fX264optWeightBSwitch && sender != fX264optBRDOSwitch && sender != fX264optBPyramidSwitch && sender != fX264optBiMESwitch && sender != fX264optDirectPredPopUp)
421         {
422             [fX264optDirectPredPopUp selectItemAtIndex: 0];
423             [[fX264optDirectPredPopUp cell] performClick:self];
424             
425         }
426     }
427     else if ( [fX264optBframesPopUp indexOfSelectedItem ] == 2)
428     {
429         /* Only 1 b-frame? Disable b-pyramid. */
430         [[fX264optBPyramidSwitch animator] setHidden:YES];
431         [[fX264optBPyramidLabel animator] setHidden:YES];
432         if ( [fX264optBPyramidSwitch state] == 1 && sender != fX264optBPyramidSwitch)
433             [fX264optBPyramidSwitch performClick:self];
434
435         [[fX264optWeightBSwitch animator] setHidden:NO];
436         [[fX264optWeightBLabel animator] setHidden:NO];
437
438         [[fX264optBiMESwitch animator] setHidden:NO];
439         [[fX264optBiMELabel animator] setHidden:NO];
440                 
441         if ( [fX264optSubmePopUp indexOfSelectedItem] >= 7 || [fX264optSubmePopUp indexOfSelectedItem] == 0 )
442         {
443             /* Only show B-RDO if both bframes and subme allow it. */
444             [[fX264optBRDOSwitch animator] setHidden:NO];
445             [[fX264optBRDOLabel animator] setHidden:NO];
446         }
447
448         [[fX264optDirectPredPopUp animator] setHidden:NO];
449         [[fX264optDirectPredLabel animator] setHidden:NO];
450     }
451     else
452     {
453         [[fX264optWeightBSwitch animator] setHidden:NO];
454         [[fX264optWeightBLabel animator] setHidden:NO];
455
456         [[fX264optBPyramidSwitch animator] setHidden:NO];
457         [[fX264optBPyramidLabel animator] setHidden:NO];
458
459         [[fX264optBiMESwitch animator] setHidden:NO];
460         [[fX264optBiMELabel animator] setHidden:NO];
461
462         if ( [fX264optSubmePopUp indexOfSelectedItem] >= 7 || [fX264optSubmePopUp indexOfSelectedItem] == 0 )
463         {
464             /* Only show B-RDO if both bframes and subme allow it. */
465             [[fX264optBRDOSwitch animator] setHidden:NO];
466             [[fX264optBRDOLabel animator] setHidden:NO];
467         }
468
469         [[fX264optDirectPredPopUp animator] setHidden:NO];
470         [[fX264optDirectPredLabel animator] setHidden:NO];
471     }
472     
473     if ( [fX264optCabacSwitch state] == false)
474     {
475         /* Without CABAC entropy coding, trellis doesn't run. */
476         
477         [[fX264optTrellisPopUp animator] setHidden:YES];
478         [[fX264optTrellisLabel animator] setHidden:YES];
479         [fX264optTrellisPopUp selectItemAtIndex:0];
480         if (sender != fX264optTrellisPopUp)
481             [[fX264optTrellisPopUp cell] performClick:self];
482     }
483     else
484     {
485         [[fX264optTrellisPopUp animator] setHidden:NO];
486         [[fX264optTrellisLabel animator] setHidden:NO];
487     }
488     
489     if ( [fX264optSubmePopUp indexOfSelectedItem] < 7 && [fX264optSubmePopUp indexOfSelectedItem] != 0 )
490     {
491         /* When subme < 6, B-RDO doesn't work. */
492         [[fX264optBRDOSwitch animator] setHidden:YES];
493         [[fX264optBRDOLabel animator] setHidden:YES];
494         if ( [fX264optBRDOSwitch state] == 1 && sender != fX264optBRDOSwitch )
495             [fX264optBRDOSwitch performClick:self];
496     }
497     else if ( [fX264optBframesPopUp indexOfSelectedItem ] >= 2 )
498     {
499         /* Make sure to only display B-RDO if allowed by both
500            the subme and bframe option settings.               */
501         [[fX264optBRDOSwitch animator] setHidden:NO]; 
502         [[fX264optBRDOLabel animator] setHidden:NO]; 
503     }
504     
505     if ( [fX264optAnalysePopUp indexOfSelectedItem] == 1)
506     {
507         /* No analysis? Disable 8x8dct */
508         [[fX264opt8x8dctSwitch animator] setHidden:YES];
509         [[fX264opt8x8dctLabel animator] setHidden:YES];
510         if ( [fX264opt8x8dctSwitch state] == 1 && sender != fX264opt8x8dctSwitch )
511             [fX264opt8x8dctSwitch performClick:self];
512     }
513     else
514     {
515         [[fX264opt8x8dctSwitch animator] setHidden:NO];
516         [[fX264opt8x8dctLabel animator] setHidden:NO];
517     }
518     
519     if ( [fX264optRefPopUp indexOfSelectedItem] < 3)
520     {
521         /* Only do mixed-refs when there are at least 2 refs to mix. */
522         [[fX264optMixedRefsSwitch animator] setHidden:YES];
523         [[fX264optMixedRefsLabel animator] setHidden:YES];
524         if ( [fX264optMixedRefsSwitch state] == 1 && sender != fX264optMixedRefsSwitch )
525             [fX264optMixedRefsSwitch performClick:self];
526     }
527     else
528     {
529         [[fX264optMixedRefsSwitch animator] setHidden:NO];
530         [[fX264optMixedRefsLabel animator] setHidden:NO];
531     }
532 }
533
534 /**
535  * Resets the GUI widgets to the contents of the option string.
536  */
537 - (IBAction) X264AdvancedOptionsSetCurrentSettings: (id) sender
538 {
539     /* Set widgets depending on the opt string in field */
540     NSString * thisOpt; // The separated option such as "bframes=3"
541     NSString * optName = @""; // The option name such as "bframes"
542     NSString * optValue = @"";// The option value such as "3"
543     NSArray *currentOptsArray;
544     
545     /*First, we get an opt string to process */
546     NSString *currentOptString = [fDisplayX264Options stringValue];
547     
548     /* Verify there is an opt string to process by making sure an
549        option is getting its value set. If so, start to process it. */
550     NSRange currentOptRange = [currentOptString rangeOfString:@"="];
551     if (currentOptRange.location != NSNotFound)
552     {
553         /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/
554         currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
555         
556         /*iterate through the array and get <opts> and <values*/
557         int loopcounter;
558         int currentOptsArrayCount = [currentOptsArray count];
559         for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
560         {
561             thisOpt = [currentOptsArray objectAtIndex:loopcounter];
562             
563             /* Verify the option sets a value */
564             NSRange splitOptRange = [thisOpt rangeOfString:@"="];            
565             if (splitOptRange.location != NSNotFound)
566             {
567                 /* Split thisOpt into an optName setting an optValue. */
568                 optName = [thisOpt substringToIndex:splitOptRange.location];
569                 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
570                 
571                 /*Run through the available widgets for x264 opts and set them, as you add widgets, 
572                     they need to be added here. This should be moved to its own method probably*/
573                 
574                 /*bframes NSPopUpButton*/
575                 if ([optName isEqualToString:@"bframes"])
576                 {
577                     [fX264optBframesPopUp selectItemAtIndex:[optValue intValue]+1];
578                 }
579                 /*ref NSPopUpButton*/
580                 if ([optName isEqualToString:@"ref"])
581                 {
582                     [fX264optRefPopUp selectItemAtIndex:[optValue intValue]+1];
583                 }
584                 /*No Fast PSkip NSButton*/
585                 if ([optName isEqualToString:@"no-fast-pskip"])
586                 {
587                     [fX264optNfpskipSwitch setState:[optValue intValue]];
588                 }
589                 /*No Dict Decimate NSButton*/
590                 if ([optName isEqualToString:@"no-dct-decimate"])
591                 {
592                     [fX264optNodctdcmtSwitch setState:[optValue intValue]];
593                 }
594                 /*Sub Me NSPopUpButton*/
595                 if ([optName isEqualToString:@"subq"])
596                 {
597                     [fX264optSubmePopUp selectItemAtIndex:[optValue intValue]+1];
598                 }
599                 /*Trellis NSPopUpButton*/
600                 if ([optName isEqualToString:@"trellis"])
601                 {
602                     [fX264optTrellisPopUp selectItemAtIndex:[optValue intValue]+1];
603                 }
604                 /*Mixed Refs NSButton*/
605                 if ([optName isEqualToString:@"mixed-refs"])
606                 {
607                     [fX264optMixedRefsSwitch setState:[optValue intValue]];
608                 }
609                 /*Motion Estimation NSPopUpButton*/
610                 if ([optName isEqualToString:@"me"])
611                 {
612                     if ([optValue isEqualToString:@"dia"])
613                         [fX264optMotionEstPopUp selectItemAtIndex:1];
614                     else if ([optValue isEqualToString:@"hex"])
615                         [fX264optMotionEstPopUp selectItemAtIndex:2];
616                     else if ([optValue isEqualToString:@"umh"])
617                         [fX264optMotionEstPopUp selectItemAtIndex:3];
618                     else if ([optValue isEqualToString:@"esa"])
619                         [fX264optMotionEstPopUp selectItemAtIndex:4];                        
620                 }
621                 /*ME Range NSPopUpButton*/
622                 if ([optName isEqualToString:@"merange"])
623                 {
624                     [fX264optMERangePopUp selectItemAtIndex:[optValue intValue]-3];
625                 }
626                 /*Weighted B-Frames NSButton*/
627                 if ([optName isEqualToString:@"weightb"])
628                 {
629                     [fX264optWeightBSwitch setState:[optValue intValue]];
630                 }
631                 /*BRDO NSButton*/
632                 if ([optName isEqualToString:@"brdo"])
633                 {
634                     [fX264optBRDOSwitch setState:[optValue intValue]];
635                 }
636                 /*B Pyramid NSPButton*/
637                 if ([optName isEqualToString:@"b-pyramid"])
638                 {
639                     [fX264optBPyramidSwitch setState:[optValue intValue]];
640                 }
641                 /*Bidirectional Motion Estimation Refinement NSButton*/
642                 if ([optName isEqualToString:@"bime"])
643                 {
644                     [fX264optBiMESwitch setState:[optValue intValue]];
645                 }
646                 /*Direct B-frame Prediction NSPopUpButton*/
647                 if ([optName isEqualToString:@"direct"])
648                 {
649                     if ([optValue isEqualToString:@"none"])
650                         [fX264optDirectPredPopUp selectItemAtIndex:1];
651                     else if ([optValue isEqualToString:@"spatial"])
652                         [fX264optDirectPredPopUp selectItemAtIndex:2];
653                     else if ([optValue isEqualToString:@"temporal"])
654                         [fX264optDirectPredPopUp selectItemAtIndex:3];
655                     else if ([optValue isEqualToString:@"auto"])
656                         [fX264optDirectPredPopUp selectItemAtIndex:4];                        
657                 }
658                 /*Deblocking NSPopUpButtons*/
659                 if ([optName isEqualToString:@"deblock"])
660                 {
661                     NSString * alphaDeblock = @"";
662                     NSString * betaDeblock = @"";
663                     
664                     NSRange splitDeblock = [optValue rangeOfString:@","];
665                     alphaDeblock = [optValue substringToIndex:splitDeblock.location];
666                     betaDeblock = [optValue substringFromIndex:splitDeblock.location + 1];
667                     
668                     if ([alphaDeblock isEqualToString:@"0"] && [betaDeblock isEqualToString:@"0"])
669                     {
670                         /* When both filters are at 0, default */
671                         [fX264optAlphaDeblockPopUp selectItemAtIndex:0];                        
672                         [fX264optBetaDeblockPopUp selectItemAtIndex:0];                               
673                     }
674                     else
675                     {
676                         if (![alphaDeblock isEqualToString:@"0"])
677                         {
678                             /* Alpha isn't 0, so set it. The offset of 7 is
679                                because filters start at -6 instead of at 0. */
680                             [fX264optAlphaDeblockPopUp selectItemAtIndex:[alphaDeblock intValue]+7];
681                         }
682                         else
683                         {
684                             /* Set alpha filter to 0, which is 7 up
685                                because filters start at -6, not 0. */
686                             [fX264optAlphaDeblockPopUp selectItemAtIndex:7];                        
687                         }
688                         
689                         if (![betaDeblock isEqualToString:@"0"])
690                         {
691                             /* Beta isn't 0, so set it. */
692                             [fX264optBetaDeblockPopUp selectItemAtIndex:[betaDeblock intValue]+7];
693                         }
694                         else
695                         {
696                             /* Set beta filter to 0. */
697                             [fX264optBetaDeblockPopUp selectItemAtIndex:7];                        
698                         }
699                     }
700                 }
701                 /* Analysis NSPopUpButton */
702                 if ([optName isEqualToString:@"analyse"])
703                 {
704                     if ([optValue isEqualToString:@"p8x8,b8x8,i8x8,i4x4"])
705                     {
706                         /* Default ("some") */
707                         [fX264optAnalysePopUp selectItemAtIndex:0];
708                     }
709                     if ([optValue isEqualToString:@"none"])
710                     {
711                         [fX264optAnalysePopUp selectItemAtIndex:1];
712                     }
713                     if ([optValue isEqualToString:@"all"])
714                     {
715                         [fX264optAnalysePopUp selectItemAtIndex:2];
716                     }
717                 }
718                 /* 8x8 DCT NSButton */
719                 if ([optName isEqualToString:@"8x8dct"])
720                 {
721                     [fX264opt8x8dctSwitch setState:[optValue intValue]];
722                 }
723                 /* CABAC NSButton */
724                 if ([optName isEqualToString:@"cabac"])
725                 {
726                     [fX264optCabacSwitch setState:[optValue intValue]];
727                 }                                                                 
728             }
729         }
730     }
731 }
732
733 /**
734  * Resets the option string to mirror the GUI widgets.
735  */
736 - (IBAction) X264AdvancedOptionsChanged: (id) sender
737 {
738     /*Determine which outlet is being used and set optName to process accordingly */
739     NSString * optNameToChange = @""; // The option name such as "bframes"
740     
741     if (sender == fX264optBframesPopUp)
742     {
743         optNameToChange = @"bframes";
744     }
745     if (sender == fX264optRefPopUp)
746     {
747         optNameToChange = @"ref";
748     }
749     if (sender == fX264optNfpskipSwitch)
750     {
751         optNameToChange = @"no-fast-pskip";
752     }
753     if (sender == fX264optNodctdcmtSwitch)
754     {
755         optNameToChange = @"no-dct-decimate";
756     }
757     if (sender == fX264optSubmePopUp)
758     {
759         optNameToChange = @"subq";
760     }
761     if (sender == fX264optTrellisPopUp)
762     {
763         optNameToChange = @"trellis";
764     }
765     if (sender == fX264optMixedRefsSwitch)
766     {
767         optNameToChange = @"mixed-refs";
768     }
769     if (sender == fX264optMotionEstPopUp)
770     {
771         optNameToChange = @"me";
772     }
773     if (sender == fX264optMERangePopUp)
774     {
775         optNameToChange = @"merange";
776     }
777     if (sender == fX264optWeightBSwitch)
778     {
779         optNameToChange = @"weightb";
780     }
781     if (sender == fX264optBRDOSwitch)
782     {
783         optNameToChange = @"brdo";
784     }
785     if (sender == fX264optBPyramidSwitch)
786     {
787         optNameToChange = @"b-pyramid";
788     }
789     if (sender == fX264optBiMESwitch)
790     {
791         optNameToChange = @"bime";
792     }
793     if (sender == fX264optDirectPredPopUp)
794     {
795         optNameToChange = @"direct";
796     }
797     if (sender == fX264optAlphaDeblockPopUp)
798     {
799         optNameToChange = @"deblock";
800     }
801     if (sender == fX264optBetaDeblockPopUp)
802     {
803         optNameToChange = @"deblock";
804     }        
805     if (sender == fX264optAnalysePopUp)
806     {
807         optNameToChange = @"analyse";
808     }
809     if (sender == fX264opt8x8dctSwitch)
810     {
811         optNameToChange = @"8x8dct";
812     }
813     if (sender == fX264optCabacSwitch)
814     {
815         optNameToChange = @"cabac";
816     }
817     
818     /* Set widgets depending on the opt string in field */
819     NSString * thisOpt; // The separated option such as "bframes=3"
820     NSString * optName = @""; // The option name such as "bframes"
821     NSString * optValue = @"";// The option value such as "3"
822     NSArray *currentOptsArray;
823     
824     /*First, we get an opt string to process */
825     NSString *currentOptString = [fDisplayX264Options stringValue];
826     
827     /* There are going to be a few possibilities.
828        - The option might start off the string.
829        - The option might be in the middle of the string.
830        - The option might not be in the string at all yet.
831        - The string itself might not yet exist.
832        
833        Because each of these possibilities means constructing a different kind of string,
834        they're all handled separately in a sea of messy, somewhat redundant code. =(     */
835        
836     /* If the option is in the string but not the beginning of it, it will be in the form of ":optName=value"
837        so we really want to be looking for ":optNameToChange=" rather than "optNameToChange".                 */
838     NSString *checkOptNameToChange = [NSString stringWithFormat:@":%@=",optNameToChange];
839     
840     /* Now we store the part of the string up through the option name in currentOptRange. */
841     NSRange currentOptRange = [currentOptString rangeOfString:checkOptNameToChange];
842
843     /*  We need to know if the option is at the beginning of the string.
844         If it is at the start, it won't be preceded by a colon.
845         To figure this out, we'll use the rangeOfString method. First,
846         store what the option name would be if if it was at the beginning,
847         in checkOptNameToChangeBeginning. Then, find its range in the string.
848         If the range is 0, it's the first option listed in the string.       */        
849     NSString *checkOptNameToChangeBeginning = [NSString stringWithFormat:@"%@=",optNameToChange];
850     NSRange currentOptRangeBeginning = [currentOptString rangeOfString:checkOptNameToChangeBeginning];
851     
852     if (currentOptRange.location != NSNotFound || currentOptRangeBeginning.location == 0)
853     {
854         /* If the option is in the string wth a semicolon, or starts the string, it's time to edit.
855            This means parsing the whole string into an array of options and values. From there,
856            iterate through the options, and when you reach the one that's been changed, edit it.   */
857         
858         /* Create new empty opt string*/
859         NSString *changedOptString = @"";
860         
861         /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/
862         currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
863         
864         /*iterate through the array and get <opts> and <values*/
865         int loopcounter;
866         int currentOptsArrayCount = [currentOptsArray count];
867         for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
868         {
869             thisOpt = [currentOptsArray objectAtIndex:loopcounter];
870             NSRange splitOptRange = [thisOpt rangeOfString:@"="];
871             
872             if (splitOptRange.location != NSNotFound)
873             {
874                 /* First off, it's time to handle option strings that
875                    already have at least one option=value pair in them. */
876                    
877                 optName = [thisOpt substringToIndex:splitOptRange.location];
878                 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
879                 
880                 /*Run through the available widgets for x264 opts and set them, as you add widgets, 
881                     they need to be added here. This should be moved to its own method probably*/
882                 
883                 /*If the optNameToChange is found, appropriately change the value or delete it if
884                     "Unspecified" is set.*/
885                 if ([optName isEqualToString:optNameToChange])
886                 {
887                     if ([optNameToChange isEqualToString:@"deblock"])
888                     {
889                         if ((([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 7)) && (([fX264optBetaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optBetaDeblockPopUp indexOfSelectedItem] == 7)))
890                         {
891                             /* When both deblock widgets are 0 or default or a mix of the two,
892                                use a blank string, since deblocking defaults to 0,0.           */
893                             thisOpt = @"";                                
894                         }
895                         else
896                         {
897                             /* Otherwise the format is deblock=a,b, where a and b both have an array
898                                offset of 7 because deblocking values start at -6 instead of at zero. */
899                             thisOpt = [NSString stringWithFormat:@"%@=%d,%d",optName, ([fX264optAlphaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optAlphaDeblockPopUp indexOfSelectedItem]-7 : 0,([fX264optBetaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optBetaDeblockPopUp indexOfSelectedItem]-7 : 0];
900                         }
901                     }
902                     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"] )
903                     {
904                         /* Here is where we take care of the boolean options that work overtly:
905                            no-dct-decimate being on means no-dct-decimate=1, etc. Some options
906                            require the inverse, but those will be handled a couple lines down. */
907                         if ([sender state] == 0)
908                         {
909                             /* When these options are false, don't include them. They all default
910                                to being set off, so they don't need to be mentioned at all.       */
911                             thisOpt = @"";
912                         }
913                         else
914                         {
915                             /* Otherwise, include them as optioname=1 */
916                             thisOpt = [NSString stringWithFormat:@"%@=%d",optName,1];
917                         }
918                     }
919                     else if ([optNameToChange isEqualToString:@"cabac"])
920                     {
921                         /* CABAC is odd, in that it defaults to being on. That means
922                            it only needs to be included in the string when turned off. */
923                         if ([sender state] == 1)
924                         {
925                             /* It's true so don't include it. */
926                             thisOpt = @"";
927                         }
928                         else
929                         {
930                             /* Otherwise, include cabac=0 in the string to enable CAVLC. */
931                             thisOpt = [NSString stringWithFormat:@"%@=%d",optName,0];
932                         }
933                     }                                        
934                     else if (([sender indexOfSelectedItem] == 0) && (sender != fX264optAlphaDeblockPopUp) && (sender != fX264optBetaDeblockPopUp) ) // means that "unspecified" is chosen, lets then remove it from the string
935                     {
936                         /* When a widget is at index 0, it's default. Default means don't add to the string.
937                            The exception for deblocking is because for those, *both* need to at index 0
938                            for it to default, so it's handled separately, above this section.                */
939                         thisOpt = @"";
940                     }
941                     else if ([optNameToChange isEqualToString:@"me"])
942                     {
943                         /* Motion estimation uses string values, so this switch
944                            pairs the widget index with the right value string.  */
945                         switch ([sender indexOfSelectedItem])
946                         {   
947                             case 1:
948                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"dia"];
949                                 break;
950                                 
951                             case 2:
952                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"hex"];
953                                 break;
954                                 
955                             case 3:
956                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"umh"];
957                                 break;
958                                 
959                             case 4:
960                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"esa"];
961                                 break;
962                                 
963                             default:
964                                 break;
965                         }
966                     }
967                     else if ([optNameToChange isEqualToString:@"direct"])
968                     {
969                         /* Direct prediction uses string values, so this switch
970                            pairs the right string value with the right widget index. */
971                         switch ([sender indexOfSelectedItem])
972                         {   
973                             case 1:
974                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
975                                 break;
976                                 
977                             case 2:
978                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"spatial"];
979                                 break;
980                                 
981                             case 3:
982                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"temporal"];
983                                 break;
984                                 
985                             case 4:
986                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"auto"];
987                                 break;
988                                 
989                             default:
990                                 break;
991                         }
992                     }
993                     else if ([optNameToChange isEqualToString:@"analyse"])
994                     {
995                         /* Analysis uses string values as well. */
996                         switch ([sender indexOfSelectedItem])
997                         {   
998                             case 1:
999                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
1000                                 break;
1001                                 
1002                             case 2:
1003                                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"all"];
1004                                 break;
1005                                 
1006                             default:
1007                                 break;
1008                         }
1009                     }
1010                     else if ([optNameToChange isEqualToString:@"merange"])
1011                     {
1012                         /* Motion estimation range uses an odd array offset because in addition
1013                            to starting with index 0 as default, index 1 starts at 4 instead of 1,
1014                            because merange can't go below 4. So it has to be handled separately.  */
1015                         thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]+3];
1016                     }
1017                     else // we have a valid value to change, so change it
1018                     {
1019                         if ( [sender indexOfSelectedItem] != 0 )
1020                         /* Here's our general case, that catches things like ref frames and b-frames.
1021                            Basically, any options that are PopUp menus with index 0 as default and
1022                            index 1 as 1, with numerical values, are all handled right here. All of
1023                            the above stuff is for the exceptions to the general case.              */
1024                             thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]-1];
1025                     }
1026                 }
1027             }
1028             
1029             /* Construct New String for opts here */
1030             if ([thisOpt isEqualToString:@""])
1031             {
1032                 /* Blank option, so just add it to the string. (Why?) */
1033                 changedOptString = [NSString stringWithFormat:@"%@%@",changedOptString,thisOpt];
1034             }
1035             else
1036             {
1037                 if ([changedOptString isEqualToString:@""])
1038                 {
1039                     /* No existing string, make the string this option. */
1040                     changedOptString = [NSString stringWithFormat:@"%@",thisOpt];
1041                 }
1042                 else
1043                 {
1044                     /* Existing string, existing option. Append the
1045                        option to the string, preceding it with a colon. */
1046                     changedOptString = [NSString stringWithFormat:@"%@:%@",changedOptString,thisOpt];
1047                 }
1048             }
1049         }
1050         
1051         /* Change the dislayed option string to reflect the new modified settings */
1052         [fDisplayX264Options setStringValue:[NSString stringWithFormat:changedOptString]];    
1053     }
1054     else // if none exists, add it to the string
1055     {
1056         /* This is where options that aren't already in the string are handled. */
1057         if ([[fDisplayX264Options stringValue] isEqualToString: @""])
1058         {
1059             /* The option might not be in the string because the
1060                string is empty. Handle this possibility first.   */
1061             if ([optNameToChange isEqualToString:@"me"])
1062             {
1063                 /* Special case for motion estimation, which uses string values
1064                    that need to be paired up with the equivalent widget index.  */
1065                 switch ([sender indexOfSelectedItem])
1066                 {   
1067                     case 1:
1068                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1069                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"dia"]]];
1070                         break;
1071                         
1072                     case 2:
1073                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1074                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"hex"]]];
1075                         break;
1076                         
1077                     case 3:
1078                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1079                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"umh"]]];
1080                         break;
1081                         
1082                     case 4:
1083                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1084                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"esa"]]];
1085                         break;
1086                         
1087                     default:
1088                         break;
1089                 }
1090             }
1091             else if ([optNameToChange isEqualToString:@"direct"])
1092             {
1093                 /* Special case for direct prediction, which uses string values
1094                    that need to be paired up with the equivalent widget index.  */
1095                 switch ([sender indexOfSelectedItem])
1096                 {   
1097                     case 1:
1098                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1099                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
1100                         break;
1101                         
1102                     case 2:
1103                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1104                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"spatial"]]];
1105                         break;
1106                         
1107                     case 3:
1108                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1109                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"temporal"]]];
1110                         break;
1111                         
1112                     case 4:
1113                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1114                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"auto"]]];
1115                         break;
1116                         
1117                     default:
1118                         break;
1119                 }
1120             }
1121             else if ([optNameToChange isEqualToString:@"analyse"])
1122             {
1123                 /* Special case for partition analysis, which uses string values
1124                    that need to be paired up with the equivalent widget index.  */
1125                 switch ([sender indexOfSelectedItem])
1126                 {   
1127                     case 1:
1128                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1129                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
1130                         break;
1131                         
1132                     case 2:
1133                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1134                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"all"]]];
1135                         break;
1136                         
1137                     default:
1138                         break;
1139                 }
1140             }
1141             
1142             else if ([optNameToChange isEqualToString:@"merange"])
1143             {
1144                 /* Special case for motion estimation range, which uses
1145                    a widget index offset of 3. This is because the
1146                    first valid value after default is four, not zero.   */
1147                 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1148                     [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]+3]]];
1149             }
1150             else if ([optNameToChange isEqualToString:@"deblock"])
1151             {
1152                 /* Very special case for deblock. Uses a weird widget index offset
1153                    of 7, because the first value after default is -6, rather than 0.
1154                    As well, deblock only goes to default when *both* alpha and beta
1155                    are zero. If only one is zero, you can't mark it down as default.
1156                    Instead, mark that one down as literally 0. This is because when
1157                    widgets are at default values, they aren't included in the string.
1158                    If only one filter is at 0, both need to be overtly specified.    */
1159                 [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]]];                
1160             }
1161             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"] )
1162             {
1163                 /* This covers all the boolean options that need to be specified only when true. */
1164                 if ([sender state] == 0)
1165                 {
1166                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@""]];                    
1167                 }
1168                 else
1169                 {
1170                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1171                         [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];
1172                 }
1173             }
1174             else if ([optNameToChange isEqualToString:@"cabac"])
1175             {
1176                 /* CABAC is weird in that you need the inverse. Only include in the string
1177                    when cabac=0, because cabac=1 is the default. Turning it off means CAVLC. */
1178                 if ([sender state] == 1)
1179                 {
1180                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@""]];                                        
1181                 }
1182                 else
1183                 {
1184                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1185                         [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];                    
1186                 }
1187             }            
1188             else
1189             {
1190                 if ( [sender indexOfSelectedItem] != 0 )
1191                 /* General case to cover all the normal PopUp widgets, like ref and b-frames. */
1192                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
1193                     [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]-1]]];
1194             }
1195         }
1196         else
1197         {
1198             /* The string isn't empty, and the option isn't already in it,
1199                so it will need to be appended to the string with a colon.  */
1200             if ([optNameToChange isEqualToString:@"me"])
1201             {
1202                 /* Special case for motion estimation, which uses string values
1203                    that need to be paired up with the equivalent widget index.  */
1204                 switch ([sender indexOfSelectedItem])
1205                 {   
1206                     case 1:
1207                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1208                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1209                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"dia"]]];
1210                         break;
1211                         
1212                     case 2:
1213                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1214                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1215                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"hex"]]];
1216                         break;
1217                         
1218                     case 3:
1219                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1220                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1221                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"umh"]]];
1222                         break;
1223                         
1224                     case 4:
1225                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1226                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1227                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"esa"]]];
1228                         break;
1229                         
1230                     default:
1231                         break;
1232                 }
1233             }
1234             else if ([optNameToChange isEqualToString:@"direct"])
1235             {
1236                 /* Special case for direct prediction, which uses string values
1237                    that need to be paired up with the equivalent widget index.  */
1238                 switch ([sender indexOfSelectedItem])
1239                 {   
1240                     case 1:
1241                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1242                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1243                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
1244                         break;
1245                         
1246                     case 2:
1247                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1248                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1249                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"spatial"]]];
1250                         break;
1251                         
1252                     case 3:
1253                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1254                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1255                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"temporal"]]];
1256                         break;
1257                         
1258                     case 4:
1259                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1260                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1261                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"auto"]]];
1262                         break;
1263                         
1264                     default:
1265                         break;
1266                 }
1267             }
1268             else if ([optNameToChange isEqualToString:@"analyse"])
1269             {
1270                 /* Special case for partition analysis, which uses string values
1271                    that need to be paired up with the equivalent widget index.  */
1272                 switch ([sender indexOfSelectedItem])
1273                 {   
1274                     case 1:
1275                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1276                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1277                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
1278                         break;
1279                         
1280                     case 2:
1281                         [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
1282                             [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1283                             [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"all"]]];
1284                         break;
1285                         
1286                     default:
1287                         break;
1288                 }
1289             }
1290             
1291             else if ([optNameToChange isEqualToString:@"merange"])
1292             {
1293                 /* Motion estimation range uses a weird offset since its index goes
1294                    0: default, 1: 4, because the first valid value is 4, not 1.     */
1295                 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]], 
1296                     [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]+3]]];
1297             }
1298             else if ([optNameToChange isEqualToString:@"deblock"])
1299             {
1300                 /* Deblock is really weird because it has two values, and if only one is default, both
1301                    still need to be specified directly. with the default one at zero. To make deblock
1302                    just a little more fun, values start at -6 instead of at zero.                       */
1303                 [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]]];                
1304             }
1305             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"] )
1306             {
1307                 /* Covers all the normal booleans, that only need to be included in the string when they're true. */
1308                 if ([sender state] == 0)
1309                 {
1310                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]]]];                    
1311                 }
1312                 else
1313                 {
1314                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]], 
1315                         [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];                
1316                 }
1317             }
1318             else if ([optNameToChange isEqualToString:@"cabac"])
1319             {
1320                 /* CABAC is weird, in that it's an inverse. Only include it in the string when it's false. */
1321                 if ([sender state] == 1)
1322                 {
1323                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]]]];                    
1324                 }
1325                 else
1326                 {
1327                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]], 
1328                         [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];
1329                 }
1330             }
1331             else
1332             {
1333                 /* General case to handle the normal PopUp widgets like ref and b-frames. */
1334                 if ( [sender indexOfSelectedItem] != 0 )
1335                     [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]], 
1336                     [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]-1]]];
1337             }
1338         }
1339     }
1340     
1341     /* We now need to reset the opt widgets since we changed some stuff */        
1342     [self X264AdvancedOptionsSet:sender];        
1343 }
1344
1345 @end