OSDN Git Service

import 0.9.4
[handbrake-jp/handbrake-jp.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, fX264optBPyramidLabel,fX264optBPyramidSwitch,
72         fX264optDirectPredLabel,fX264optDirectPredPopUp,fX264optDeblockLabel,fX264optAnalyseLabel,
73         fX264optAnalysePopUp,fX264opt8x8dctLabel,fX264opt8x8dctSwitch,fX264optCabacLabel,fX264optCabacSwitch,
74         fX264optAlphaDeblockPopUp,fX264optBetaDeblockPopUp, fX264optPsyRDSlider, fX264optPsyRDLabel, fX264optPsyTrellisSlider, fX264optPsyTrellisLabel, fX264optBAdaptPopUp, fX264optBAdaptLabel };
75
76     for( i = 0; i < sizeof( controls ) / sizeof( NSControl * ); i++ )
77     {
78         if( [[controls[i] className] isEqualToString: @"NSTextField"] )
79         {
80             NSTextField * tf = (NSTextField *) controls[i];
81             if( ![tf isBezeled] )
82             {
83                 [tf setTextColor: b ? [NSColor controlTextColor] :
84                     [NSColor disabledControlTextColor]];
85                 continue;
86             }
87         }
88         [controls[i] setEnabled: b];
89
90     }
91     
92     [fX264optView setWantsLayer:YES];
93 }
94
95 - (void)dealloc
96 {
97     [super dealloc];
98 }
99
100 /**
101  * Populates the option widgets
102  */
103 - (IBAction) X264AdvancedOptionsSet: (id) sender
104 {
105     /*Set opt widget values here*/
106     
107     NSString * toolTip = @"";
108     
109     /*B-Frames fX264optBframesPopUp*/
110     int i;
111     [fX264optBframesPopUp removeAllItems];
112     [fX264optBframesPopUp addItemWithTitle:@"Default (3)"];
113     for (i=0; i<17;i++)
114     {
115         [fX264optBframesPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
116     }
117     toolTip =
118         @"Sane values are 1-6. B-Frames are smaller than other frames, so they let you pack in more quality at the same bitrate. Use more of them with animated material.";
119     [fX264optBframesPopUp setToolTip: toolTip];
120     [fX264optBframesLabel setToolTip: toolTip];
121     
122     /*Reference Frames fX264optRefPopUp*/
123     [fX264optRefPopUp removeAllItems];
124     [fX264optRefPopUp addItemWithTitle:@"Default (3)"];
125     for (i=0; i<17;i++)
126     {
127         [fX264optRefPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
128     }
129     toolTip =
130         @"Sane values are 1-6. The more you add, the higher the quality — but the slower the encode. Be careful...too many and QuickTime struggle to play the video back.";
131     [fX264optRefPopUp setToolTip: toolTip];
132     [fX264optRefLabel setToolTip: toolTip];
133     
134     /*No Fast P-Skip fX264optNfpskipSwitch BOOLEAN*/
135     [fX264optNfpskipSwitch setState:0];
136     toolTip =
137         @"This can help with blocking on solid colors like blue skies, but it also slows down the encode.";
138     [fX264optNfpskipSwitch setToolTip: toolTip];
139     [fX264optNfpskipLabel setToolTip: toolTip];
140     
141     /*No Dict Decimate fX264optNodctdcmtSwitch BOOLEAN*/
142     [fX264optNodctdcmtSwitch setState:0];    
143     toolTip =
144         @"To save space, x264 will \"zero out\" blocks when it thinks they won't be perceptible by the viewer. This negligibly reduces quality, but in rare cases it can mess up and produce visible artifacts. This situation can be alleviated by telling x264 not to decimate DCT blocks.\n\nIt increases quality but also bitrate/file size, so if you use it when you've specified a target bitrate you will end up with a worse picture than without it. However, when used with constant quality encoding, or if you boost the average bitrate to compensate, you might get a better result.";
145     [fX264optNodctdcmtSwitch setToolTip: toolTip];
146     [fX264optNodctdcmtLabel setToolTip: toolTip];
147     
148     /*Sub Me fX264optSubmePopUp*/
149     [fX264optSubmePopUp removeAllItems];
150     [fX264optSubmePopUp addItemWithTitle:@"Default (7)"];
151     for (i=0; i<10;i++)
152     {
153         [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
154     }
155     toolTip =
156         @"This setting is finer-grained than the motion estimation settings above. Instead of dealing with whole pixels, it deals with 4 fractional pixels, or quarter pixels (qpel). Higher levels increase quality by further refining the motion prediction for these quarter pixels, but take longer to encode.\n\nLevel 6, turns on a feature called rate distortion optimization, including psychovisual enhancements. 7, the default, enables that rate distortion for B-frames. 8 refines those decisions for I and P frames, and 9 adds on refinement for B-frames as well.";
157     [fX264optSubmePopUp setToolTip: toolTip];
158     [fX264optSubmeLabel setToolTip: toolTip];
159     
160     /*Trellis fX264optTrellisPopUp*/
161     [fX264optTrellisPopUp removeAllItems];
162     [fX264optTrellisPopUp addItemWithTitle:@"Default (1)"];
163     for (i=0; i<3;i++)
164     {
165         [fX264optTrellisPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
166     }
167     [fX264optTrellisPopUp setWantsLayer:YES];
168     toolTip =
169         @"Trellis fine-tunes how bitrate is doled out, so it can reduce file size/bitrate or increase quality. A value of 1 means it only fine-tunes the final encode of a block of pixels, while 2 means it is considered during earlier phases of the decision-making process as well.";
170     [fX264optTrellisPopUp setToolTip: toolTip];
171     [fX264optTrellisLabel setToolTip: toolTip];
172     
173     /*Mixed-references fX264optMixedRefsSwitch BOOLEAN*/
174     [fX264optMixedRefsSwitch setState:1];
175     [fX264optMixedRefsSwitch setWantsLayer:YES];
176     toolTip =
177         @"With this on, different references can be used for different parts of each 16x16 pixel macroblock, increasing quality.";
178     [fX264optMixedRefsSwitch setToolTip: toolTip];
179     [fX264optMixedRefsLabel setToolTip: toolTip];
180     
181     /*Motion Estimation fX264optMotionEstPopUp*/
182     [fX264optMotionEstPopUp removeAllItems];
183     [fX264optMotionEstPopUp addItemWithTitle:@"Default (Hexagon)"];
184     [fX264optMotionEstPopUp addItemWithTitle:@"Diamond"];
185     [fX264optMotionEstPopUp addItemWithTitle:@"Hexagon"];
186     [fX264optMotionEstPopUp addItemWithTitle:@"Uneven Multi-Hexagon"];
187     [fX264optMotionEstPopUp addItemWithTitle:@"Exhaustive"];
188     [fX264optMotionEstPopUp addItemWithTitle:@"Transformed Exhaustive"];
189     toolTip =
190         @"Controls the motion estimation method. Motion estimation is how the encoder decides how each block of pixels in a frame has moved, compared to most similar blocks in the other frames it references. There are many ways of finding the most similar blocks, with varying speeds and accuracy.\n\nAt the most basic setting, dia, x264 will only consider a diamond-shaped region around each pixel.\n\nThe default setting, hex, is similar to dia but uses a hexagon shape.\n\nUneven multi-hexagon, umh, searches a number of different patterns across a wider area and thus is slower than hex and dia but further increases compression efficiency and quality.\n\nesa, an exhaustive search of a square around each pixel (whose size is controlled by the me-range parameter), is much slower and offers only minimal quality gains.\n\ntesa, transformed exhaustive search, which performs just as thorough a search, is slower still but offers further slight improvements to quality.";
191     [fX264optMotionEstPopUp setToolTip: toolTip];
192     [fX264optMotionEstLabel setToolTip: toolTip];
193     
194     /*Motion Estimation range fX264optMERangePopUp*/
195     [fX264optMERangePopUp removeAllItems];
196     [fX264optMERangePopUp addItemWithTitle:@"Default (16)"];
197     for (i=4; i<65;i++)
198     {
199         [fX264optMERangePopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
200     }
201     toolTip =
202         @"This range is the radius, in pixels, x264 should use for motion estimation searches. It only has an effect when you use Uneven Multi-Hexagonal, Exhaustive, or Transformed Exhaustive searching. 24, 32, and 64 are good values, with each being progressively smaller for progressively less improvement to picture quality.";
203     [fX264optMERangePopUp setToolTip: toolTip];
204     [fX264optMERangeLabel setToolTip: toolTip];
205     
206     /*Weighted B-Frame Prediction fX264optWeightBSwitch BOOLEAN*/
207     [fX264optWeightBSwitch setState:1];
208     [fX264optWeightBSwitch setWantsLayer:YES];
209     toolTip =
210         @"Sometimes x264 will base a B-frame's motion compensation on frames both before and after. With weighted B-frames, the amount of influence each frame has is related to its distance from the frame being encoded, instead of both having equal influence. The AppleTV can have issues with this.";
211     [fX264optWeightBSwitch setToolTip: toolTip];
212     [fX264optWeightBLabel setToolTip: toolTip];
213     
214     /*B-frame Pyramids fX264optBPyramidSwitch BOOLEAN*/
215     [fX264optBPyramidSwitch setState:0];
216     [fX264optBPyramidSwitch setWantsLayer:YES];
217     toolTip =
218         @"B-frame pyramids are a High Profile feature. Pyramidal B-frames mean that B-frames don't just reference surrounding reference frames — instead, it also treats a previous B-frame as a reference, improving quality/lowering bitrate at the expense of complexity. Logically, to reference an earlier B-frame, you must tell x264 to use at least 2 B-frames.";
219     [fX264optBPyramidSwitch setToolTip: toolTip];
220     [fX264optBPyramidLabel setToolTip: toolTip];
221     
222     /*Direct B-Frame Prediction Mode fX264optDirectPredPopUp*/
223     [fX264optDirectPredPopUp removeAllItems];
224     [fX264optDirectPredPopUp addItemWithTitle:@"Default (Spatial)"];
225     [fX264optDirectPredPopUp addItemWithTitle:@"None"];
226     [fX264optDirectPredPopUp addItemWithTitle:@"Spatial"];
227     [fX264optDirectPredPopUp addItemWithTitle:@"Temporal"];
228     [fX264optDirectPredPopUp addItemWithTitle:@"Automatic"];
229     [fX264optDirectPredPopUp setWantsLayer:YES];
230     toolTip =
231         @"Direct prediction tells x264 what method to use when guessing motion for certain parts of a B-frame. It can either look at other parts of the current frame (spatial) or compare against the following P-frameframe (temporal). You're best off setting this to automatic, so x264 decides which method is best on its own. Don't select none assuming it will be faster; instead it will take longer and look worse. If you're going to choose between spatial and temporal, spatial is usually better.";
232     [fX264optDirectPredPopUp setToolTip: toolTip];
233     [fX264optDirectPredLabel setToolTip: toolTip];
234     
235     /* Adaptive B-Frames Mode fX264optBAdaptPopUp */
236     [fX264optBAdaptPopUp removeAllItems];
237     [fX264optBAdaptPopUp addItemWithTitle:@"Default (Fast)"];
238     [fX264optBAdaptPopUp addItemWithTitle:@"Off"];
239     [fX264optBAdaptPopUp addItemWithTitle:@"Fast"];
240     [fX264optBAdaptPopUp addItemWithTitle:@"Optimal"];
241     [fX264optBAdaptPopUp setWantsLayer:YES];
242     toolTip =
243         @"When adaptive B-Frames are disabled, the number of B-Frames you specify is the constant length of every B-Frame sequence. When one of the adaptive modes is enabled, the number of B-Frames is treated as a maximum, with the length of each sequence varying, but never exceeding the max.\n\nFast mode takes the same amount of time no matter how many B-frames you specify. However, it doesn't always make the best decisions on how many B-Frames to use in a sequence.\n\nOptimal mode gets slower as the maximum number of B-Frames increases, but does a much better job at deciding sequence length, which can mean smaller file sizes and better quality.";
244     [fX264optBAdaptPopUp setToolTip: toolTip];
245     [fX264optBAdaptLabel setToolTip: toolTip];
246     
247     /*Alpha Deblock*/
248     [fX264optAlphaDeblockPopUp removeAllItems];
249     [fX264optAlphaDeblockPopUp addItemWithTitle:@"Default (0)"];
250     for (i=-6; i<7;i++)
251     {
252         [fX264optAlphaDeblockPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
253     }
254     toolTip =
255         @"x264 includes an in-loop deblocking filter. What this means is that blocky compression artifacts are smoothed away when you play back the video. It has two settings: strength and threshold, just like a simple filter in Photoshop.\n\nStrength controls the amount of deblocking applied to the whole frame. If you drop down below 0, you reduce the amount of blurring. Go too negative, and you'll get an effect somewhat like oversharpening an image. Go into positive values, and the image may become too soft.\n\nThreshold controls how sensitive the filter is to whether something in a block is detail that needs to be preserved: lower numbers blur details less.\n\nThe default deblocking values are 0 and 0. This does not mean zero deblocking. It means x264 will apply the regular deblocking strength and thresholds the codec authors have selected as working the best in most cases.\n\nWhile many, many people stick with the default deblocking values of 0,0, and you should never change the deblocking without disabling adaptive quantization, other people disagree. Some prefer a slightly less blurred image for live action material, and use values like -2,-1 or -2,-2. Others will raise it to 1,1 or even 3,3 for animation. While the values for each setting extend from -6 to 6, the consensus is that going below -3 or above 3 is worthless.";
256     [fX264optAlphaDeblockPopUp setToolTip: toolTip];
257     [fX264optDeblockLabel setToolTip: toolTip];
258
259     /*Beta Deblock*/
260     [fX264optBetaDeblockPopUp removeAllItems];
261     [fX264optBetaDeblockPopUp addItemWithTitle:@"Default (0)"];
262     for (i=-6; i<7;i++)
263     {
264         [fX264optBetaDeblockPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
265     }
266     [fX264optBetaDeblockPopUp setToolTip: toolTip];
267     [fX264optDeblockLabel setToolTip: toolTip];
268
269     /* Analysis fX264optAnalysePopUp */
270     [fX264optAnalysePopUp removeAllItems];
271     [fX264optAnalysePopUp addItemWithTitle:@"Default (some)"]; /* 0=default */
272     [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"None"]]; /* 1=none */
273     [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"All"]]; /* 2=all */
274     toolTip =
275         @"Analysis controls how finely x264 divides up a frame to capture detail. Full macroblocks are 16x16 pixels, but x264 can go down all the way to 4x4 blocks if it judges it necessary. By default it only breaks up key frames that much. To give x264 the freedom to make the best decisions for all frames, use \"all\" analysis. If you want to create a high profile H.264 video (which is less compatible with the world at large than main profile), also check the \"8x8 DCT blocks\" box to add yet another block size for analysis.";
276     [fX264optAnalysePopUp setToolTip: toolTip];
277     [fX264optAnalyseLabel setToolTip: toolTip];
278
279     /* 8x8 DCT fX264op8x8dctSwitch */
280     [fX264opt8x8dctSwitch setState:1];
281     [fX264opt8x8dctSwitch setWantsLayer:YES];
282     toolTip =
283         @"Checking this box lets x264 break key frames down into 8x8 blocks of pixels for analysis. This is a high profile feature of H.264, which makes it less compatible. It should slightly decrease bitrate or improve quality. Turn it on whenever possible.";
284     [fX264opt8x8dctSwitch setToolTip: toolTip];
285     [fX264opt8x8dctLabel setToolTip: toolTip];
286
287     /* CABAC fX264opCabacSwitch */
288     [fX264optCabacSwitch setState:1];
289     toolTip =
290         @"CABAC, or context adaptive binary arithmetic coding, is used by x264 to reduce the bitrate needed for a given quality by 15\%. This makes it very cool and very useful, and it should be left on whenever possible. However, it is incompatible with the iPod, and makes the AppleTV struggle. So turn it off for those.\n\nCABAC is a kind of entropy coding, which means that it compresses data by making shorthand symbols to represent long streams of data. The \"entropy\" part means that the symbols it uses the most often are the smallest. When you disable CABAC, another entropy coding scheme gets enabled, called CAVLC (context adaptive variable-length coding). CAVLC is a lot less efficient, which is why it needs 15\% more bitrate to achieve the same quality as CABAC.";
291     [fX264optCabacSwitch setToolTip: toolTip];
292     [fX264optCabacLabel setToolTip: toolTip];
293     
294     /* PsyRDO fX264optPsyRDSlider */
295     [fX264optPsyRDSlider setMinValue:0.0];
296     [fX264optPsyRDSlider setMaxValue:1.0];
297     [fX264optPsyRDSlider setTickMarkPosition:NSTickMarkBelow];
298     [fX264optPsyRDSlider setNumberOfTickMarks:11];
299     [fX264optPsyRDSlider setAllowsTickMarkValuesOnly:YES];
300     [fX264optPsyRDSlider setFloatValue:1.0];
301     toolTip =
302         @"Psychovisual Rate Distortion Optimization sure is a mouthful, isn't it? Basically, it means x264 tries to retain detail, for better quality to the human eye, as opposed to trying to maximize quality the way a computer understands it, through signal-to-noise ratios that have trouble telling apart fine detail and noise.";
303     [fX264optPsyRDSlider setToolTip: toolTip];
304     [fX264optPsyRDLabel setToolTip: toolTip];
305
306     /* PsyTrellis fX264optPsyRDSlider */
307     [fX264optPsyTrellisSlider setMinValue:0.0];
308     [fX264optPsyTrellisSlider setMaxValue:1.0];
309     [fX264optPsyTrellisSlider setTickMarkPosition:NSTickMarkBelow];
310     [fX264optPsyTrellisSlider setNumberOfTickMarks:11];
311     [fX264optPsyTrellisSlider setAllowsTickMarkValuesOnly:YES];
312     [fX264optPsyTrellisSlider setFloatValue:0.0];
313     toolTip =
314         @"Psychovisual Trellis tries to retain more sharpness and detail, but can cause artifacting. It is considered experimental, which is why it's off by default. Good values are 0.1 to 0.2.";
315     [fX264optPsyTrellisSlider setToolTip: toolTip];
316     [fX264optPsyTrellisLabel setToolTip: toolTip];
317
318     /* Standardize the option string */
319     [self X264AdvancedOptionsStandardizeOptString:nil];
320
321     /* Set Current GUI Settings based on newly standardized string */
322     [self X264AdvancedOptionsSetCurrentSettings:sender];
323
324     /* Fade out options that don't apply */
325     [self X264AdvancedOptionsAnimate: sender];
326 }
327
328 /**
329  * Cleans the option string to use a standard format of option=value
330  */
331 - (IBAction) X264AdvancedOptionsStandardizeOptString: (id) sender
332 {
333     /* Set widgets depending on the opt string in field */
334     NSString * thisOpt; // The separated option such as "bframes=3"
335     NSString * optName = @""; // The option name such as "bframes"
336     NSString * optValue = @"";// The option value such as "3"
337     NSString * changedOptString = @"";
338     NSArray *currentOptsArray;
339     
340     /*First, we get an opt string to process */
341     NSString *currentOptString = [fDisplayX264Options stringValue];
342     
343     /* Verify there is an opt string to process by making sure an
344        option is getting its value set. If so, start to process it. */
345     NSRange currentOptRange = [currentOptString rangeOfString:@"="];
346     if (currentOptRange.location != NSNotFound)
347     {
348         /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/
349         currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
350         
351         /*iterate through the array and get <opts> and <values*/
352         int loopcounter;
353         int currentOptsArrayCount = [currentOptsArray count];
354         for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
355         {
356             thisOpt = [currentOptsArray objectAtIndex:loopcounter];
357             
358             NSRange splitOptRange = [thisOpt rangeOfString:@"="];
359             if (splitOptRange.location != NSNotFound)
360             {
361                 optName = [thisOpt substringToIndex:splitOptRange.location];
362                 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
363                 
364                 /* Standardize the names here depending on whats in the string */
365                 optName = [self X264AdvancedOptionsStandardizeOptNames:optName];
366                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,optValue];    
367             }
368             else // No value given so we use a default of "1"
369             {
370                 optName = thisOpt;
371
372                 /* Standardize the names here depending on whats in the string */
373                 optName = [self X264AdvancedOptionsStandardizeOptNames:optName];
374                 thisOpt = [NSString stringWithFormat:@"%@=%d",optName,1];
375             }
376             
377             /* Construct New String for opts here.*/
378             if ([thisOpt isEqualToString:@""])
379             {
380                 /* Blank option, just add it to the string. (Why?) */
381                 changedOptString = [NSString stringWithFormat:@"%@%@",changedOptString,thisOpt];
382             }
383             else
384             {
385                 if ([changedOptString isEqualToString:@""])
386                 {
387                     /* Blank string, output the current option. */
388                     changedOptString = [NSString stringWithFormat:@"%@",thisOpt];
389                 }
390                 else
391                 {
392                     /* Option exists and string exists, so append the option
393                        to the string with a semi-colon inbetween them.       */
394                     changedOptString = [NSString stringWithFormat:@"%@:%@",changedOptString,thisOpt];
395                 }
396             }
397         }
398     }
399     
400     /* Change the option string to reflect the new standardized option string */
401     [fDisplayX264Options setStringValue:changedOptString];
402 }
403
404 /**
405  * Cleans the option string to use a standard set of option names, by conflating synonyms.
406  */
407 - (NSString *) X264AdvancedOptionsStandardizeOptNames:(NSString *) cleanOptNameString
408 {
409     /* Reference Frames */
410     if ([cleanOptNameString isEqualToString:@"ref"] || [cleanOptNameString isEqualToString:@"frameref"])
411     {
412         cleanOptNameString = @"ref";
413     }
414     
415     /*No Fast PSkip nofast_pskip*/
416     if ([cleanOptNameString isEqualToString:@"no-fast-pskip"] || [cleanOptNameString isEqualToString:@"no_fast_pskip"] || [cleanOptNameString isEqualToString:@"nofast_pskip"])
417     {
418         cleanOptNameString = @"no-fast-pskip";
419     }
420     
421     /*No Dict Decimate*/
422     if ([cleanOptNameString isEqualToString:@"no-dct-decimate"] || [cleanOptNameString isEqualToString:@"no_dct_decimate"] || [cleanOptNameString isEqualToString:@"nodct_decimate"])
423     {
424         cleanOptNameString = @"no-dct-decimate";
425     }
426     
427     /*Subme*/
428     if ([cleanOptNameString isEqualToString:@"subme"])
429     {
430         cleanOptNameString = @"subq";
431     }
432     
433     /*ME Range*/
434     if ([cleanOptNameString isEqualToString:@"me-range"] || [cleanOptNameString isEqualToString:@"me_range"])
435         cleanOptNameString = @"merange";
436     
437     /*WeightB*/
438     if ([cleanOptNameString isEqualToString:@"weight-b"] || [cleanOptNameString isEqualToString:@"weight_b"])
439     {
440         cleanOptNameString = @"weightb";
441     }
442     
443     /*B Pyramid*/
444     if ([cleanOptNameString isEqualToString:@"b_pyramid"])
445     {
446         cleanOptNameString = @"b-pyramid";
447     }
448     
449     /*Direct Prediction*/
450     if ([cleanOptNameString isEqualToString:@"direct-pred"] || [cleanOptNameString isEqualToString:@"direct_pred"])
451     {
452         cleanOptNameString = @"direct";
453     }
454     
455     /*Deblocking*/
456     if ([cleanOptNameString isEqualToString:@"filter"])
457     {
458         cleanOptNameString = @"deblock";
459     }
460     
461     /*Analysis*/
462     if ([cleanOptNameString isEqualToString:@"partitions"])
463     {
464         cleanOptNameString = @"analyse";
465     }
466     
467     return cleanOptNameString;    
468 }
469
470 /**
471  * Fades options in and out depending on whether they're available..
472  */
473 - (IBAction) X264AdvancedOptionsAnimate: (id) sender
474 {
475     /* Lots of situations to cover.
476        - B-frames (when 0 turn of b-frame specific stuff, when < 2 disable b-pyramid)
477        - CABAC (when 0 turn off trellis)
478        - analysis (if none, turn off 8x8dct)
479        - refs (under 2, disable mixed-refs)
480        - subme (if under 6, turn off psy-rd and psy-trel)
481        - trellis (if 0, turn off psy-trel)
482     */
483     
484     if( sender == fX264optBframesPopUp || sender == nil || sender == fDisplayX264Options )
485     {
486         if ( [fX264optBframesPopUp indexOfSelectedItem ] > 0 &&
487              [fX264optBframesPopUp indexOfSelectedItem ] < 2)
488         {
489             /* If the b-frame widget is at 0 or 1, the user has chosen
490                not to use b-frames at all. So disable the options
491                that can only be used when b-frames are enabled.        */
492             
493             if( [fX264optWeightBSwitch isHidden] == false)
494             {
495                 [[fX264optWeightBSwitch animator] setHidden:YES];
496                 [[fX264optWeightBLabel animator] setHidden:YES];
497                 if ( [fX264optWeightBSwitch state] == 1 )
498                     [fX264optWeightBSwitch performClick:self];
499             }
500
501             if( [fX264optBPyramidSwitch isHidden] == false )
502             {
503                 [[fX264optBPyramidSwitch animator] setHidden:YES];
504                 [[fX264optBPyramidLabel animator] setHidden:YES];
505                 if ( [fX264optBPyramidSwitch state] == 1 )
506                     [fX264optBPyramidSwitch performClick:self];
507             }
508
509             if( [fX264optDirectPredPopUp isHidden] == false )
510             {
511                 [[fX264optDirectPredPopUp animator] setHidden:YES];
512                 [[fX264optDirectPredLabel animator] setHidden:YES];
513                 if ( [fX264optDirectPredPopUp indexOfSelectedItem] > 0 )
514                 {
515                     [fX264optDirectPredPopUp selectItemAtIndex: 0];
516                     [[fX264optDirectPredPopUp cell] performClick:self];
517                 }
518             }
519
520             if( [fX264optBAdaptPopUp isHidden] == false )
521             {
522                 [[fX264optBAdaptPopUp animator] setHidden:YES];
523                 [[fX264optBAdaptLabel animator] setHidden:YES];
524                 if ( [fX264optBAdaptPopUp indexOfSelectedItem] > 0 )
525                 {
526                     [fX264optBAdaptPopUp selectItemAtIndex: 0];
527                     [[fX264optBAdaptPopUp cell] performClick:self];
528                 }
529             }
530         }
531         else if ( [fX264optBframesPopUp indexOfSelectedItem ] == 2)
532         {
533             /* Only 1 b-frame? Disable b-pyramid. */
534             if( [fX264optBPyramidSwitch isHidden] == false )
535             {
536                 [[fX264optBPyramidSwitch animator] setHidden:YES];
537                 [[fX264optBPyramidLabel animator] setHidden:YES];
538                 if ( [fX264optBPyramidSwitch state] == 1 )
539                     [fX264optBPyramidSwitch performClick:self];
540             }
541
542             if( [fX264optWeightBSwitch isHidden] == true )
543             {
544                 [[fX264optWeightBSwitch animator] setHidden:NO];
545                 [[fX264optWeightBLabel animator] setHidden:NO];
546             }
547             
548             if( [fX264optDirectPredPopUp isHidden] == true )
549             {
550                 [[fX264optDirectPredPopUp animator] setHidden:NO];
551                 [[fX264optDirectPredLabel animator] setHidden:NO];
552             }
553             
554             if( [fX264optBAdaptPopUp isHidden] == true )
555             {
556                 [[fX264optBAdaptPopUp animator] setHidden:NO];
557                 [[fX264optBAdaptLabel animator] setHidden:NO];
558             }
559         }
560         else
561         {
562             if( [fX264optBPyramidSwitch isHidden] == true )
563             {
564                 [[fX264optBPyramidSwitch animator] setHidden:NO];
565                 [[fX264optBPyramidLabel animator] setHidden:NO];
566             }
567
568             if( [fX264optWeightBSwitch isHidden] == true )
569             {
570                 [[fX264optWeightBSwitch animator] setHidden:NO];
571                 [[fX264optWeightBLabel animator] setHidden:NO];
572             }
573             
574             if( [fX264optDirectPredPopUp isHidden] == true )
575             {
576                 [[fX264optDirectPredPopUp animator] setHidden:NO];
577                 [[fX264optDirectPredLabel animator] setHidden:NO];
578             }
579             
580             if( [fX264optBAdaptPopUp isHidden] == true )
581             {
582                 [[fX264optBAdaptPopUp animator] setHidden:NO];
583                 [[fX264optBAdaptLabel animator] setHidden:NO];
584             }
585         }
586     }
587     
588     if( sender == fX264optCabacSwitch || sender == nil || sender == fDisplayX264Options )
589     {
590         if ( [fX264optCabacSwitch state] == false)
591         {
592             if( [fX264optTrellisPopUp isHidden] == false )
593             {
594                 /* Without CABAC entropy coding, trellis doesn't run. */
595                 [[fX264optTrellisPopUp animator] setHidden:YES];
596                 [[fX264optTrellisLabel animator] setHidden:YES];
597                 [fX264optTrellisPopUp selectItemAtIndex:0];
598                 [[fX264optTrellisPopUp cell] performClick:self];
599             }
600         }
601         else if( [fX264optTrellisPopUp isHidden] == true)
602         {
603             [[fX264optTrellisPopUp animator] setHidden:NO];
604             [[fX264optTrellisLabel animator] setHidden:NO];
605         }
606     }
607     
608     if( sender == fX264optAnalysePopUp || sender == nil || sender == fDisplayX264Options )
609     {
610         if ( [fX264optAnalysePopUp indexOfSelectedItem] == 1)
611         {
612             /* No analysis? Disable 8x8dct */
613             if( [fX264opt8x8dctSwitch isHidden] == false )
614             {
615                 [[fX264opt8x8dctSwitch animator] setHidden:YES];
616                 [[fX264opt8x8dctLabel animator] setHidden:YES];
617                 if ( [fX264opt8x8dctSwitch state] == 1 )
618                     [fX264opt8x8dctSwitch performClick:self];
619             }
620         }
621         else
622         {
623             if( [fX264opt8x8dctSwitch isHidden] == true )
624             {
625                 [[fX264opt8x8dctSwitch animator] setHidden:NO];
626                 [[fX264opt8x8dctLabel animator] setHidden:NO];
627             }
628         }
629     }
630     
631     if( sender == fX264optRefPopUp || sender == nil || sender == fDisplayX264Options )
632     {
633         if ( [fX264optRefPopUp indexOfSelectedItem] > 0 &&
634              [fX264optRefPopUp indexOfSelectedItem] < 3 )
635         {
636             if( [fX264optMixedRefsSwitch isHidden] == false )
637             {
638                 /* Only do mixed-refs when there are at least 2 refs to mix. */
639                 [[fX264optMixedRefsSwitch animator] setHidden:YES];
640                 [[fX264optMixedRefsLabel animator] setHidden:YES];
641                 if( [fX264optMixedRefsSwitch state] == 1 )
642                     [fX264optMixedRefsSwitch performClick:self];
643             }
644         }
645         else
646         {
647             if( [fX264optMixedRefsSwitch isHidden] == true )
648             {
649                 [[fX264optMixedRefsSwitch animator] setHidden:NO];
650                 [[fX264optMixedRefsLabel animator] setHidden:NO];
651             }
652         }
653     }
654     
655     if( sender == fX264optMotionEstPopUp || sender == nil || sender == fDisplayX264Options )
656     {
657         if ( [fX264optMotionEstPopUp indexOfSelectedItem] < 3 )
658         {
659             /* ME-range can only be above 16 if me >= umh
660               and changing it to < 16 is idiotic so hide it . */
661             if( [fX264optMERangePopUp isHidden] == false )
662             {
663                 [[fX264optMERangePopUp animator] setHidden:YES];
664                 [[fX264optMERangeLabel animator] setHidden:YES];
665                 if ( [fX264optMERangePopUp indexOfSelectedItem] > 0 )
666                 {
667                     [fX264optMERangePopUp selectItemAtIndex:0];
668                     [[fX264optMERangePopUp cell] performClick:self];
669                 }
670             }
671         }
672         else
673         {
674             if( [fX264optMERangePopUp isHidden] == true )
675             {
676                 [[fX264optMERangePopUp animator] setHidden:NO];
677                 [[fX264optMERangeLabel animator] setHidden:NO];
678             }
679         }
680     }
681     
682     if( sender == fX264optSubmePopUp || sender == nil || sender == fDisplayX264Options )
683     {
684         if( [fX264optSubmePopUp indexOfSelectedItem] != 0 && [fX264optSubmePopUp indexOfSelectedItem] < 7 )
685         {
686             /* No Psy-RDO or Psy=trel if subme < 6. */
687             if( [fX264optPsyRDSlider isHidden] == false )
688             {
689                 [[fX264optPsyRDSlider animator] setHidden:YES];
690                 [[fX264optPsyRDLabel animator] setHidden:YES];
691                 [[fX264optPsyRDSlider animator] setFloatValue:1];
692                 if ( [fX264optPsyRDSlider floatValue] < 1.0 )
693                 {
694                     [fX264optPsyRDSlider setFloatValue:1.0];
695                     [[fX264optPsyRDSlider cell] performClick:self];            
696                 }
697             }
698
699             if( [fX264optPsyTrellisSlider isHidden] == false)
700             {
701                 [[fX264optPsyTrellisSlider animator] setHidden:YES];
702                 [[fX264optPsyTrellisLabel animator] setHidden:YES];
703                 [[fX264optPsyTrellisSlider animator] setFloatValue:0];
704                 if ( [fX264optPsyTrellisSlider floatValue] > 0.0 )
705                 {
706                     [fX264optPsyTrellisSlider setFloatValue:0.0];
707                     [[fX264optPsyTrellisSlider cell] performClick:self];
708                 }
709             }
710         }
711         else
712         {
713             if( [fX264optPsyRDSlider isHidden] == true )
714             {
715                 [[fX264optPsyRDSlider animator] setHidden:NO];
716                 [[fX264optPsyRDLabel animator] setHidden:NO];
717             }
718
719             if( ( [fX264optTrellisPopUp indexOfSelectedItem] == 0 || [fX264optTrellisPopUp indexOfSelectedItem] >= 2 ) && [fX264optCabacSwitch state] == true && [fX264optPsyTrellisSlider isHidden] == true )
720             {
721                 [[fX264optPsyTrellisSlider animator] setHidden:NO];
722                 [[fX264optPsyTrellisLabel animator] setHidden:NO];
723             }
724         }
725     }
726     
727     if( sender == fX264optTrellisPopUp || sender == nil || sender == fDisplayX264Options )
728     {
729         if( [fX264optTrellisPopUp indexOfSelectedItem] > 0 && [fX264optTrellisPopUp indexOfSelectedItem] < 2 )
730         {
731             if( [fX264optPsyTrellisSlider isHidden] == false )
732             {
733                 /* No Psy-trellis without trellis. */
734                 [[fX264optPsyTrellisSlider animator] setHidden:YES];
735                 [[fX264optPsyTrellisLabel animator] setHidden:YES];
736                 [[fX264optPsyTrellisSlider animator] setFloatValue:0.0];
737                 [[fX264optPsyTrellisSlider cell] performClick:self];
738             }
739         }
740         else
741         {
742             if( ( [fX264optSubmePopUp indexOfSelectedItem] == 0 || [fX264optSubmePopUp indexOfSelectedItem] >= 7 ) && [fX264optCabacSwitch state] == true  && [fX264optPsyTrellisSlider isHidden] == true )
743             {
744                 [[fX264optPsyTrellisSlider animator] setHidden:NO];
745                 [[fX264optPsyTrellisLabel animator] setHidden:NO];
746             }
747         }
748     }
749 }
750
751 /**
752  * Resets the GUI widgets to the contents of the option string.
753  */
754 - (IBAction) X264AdvancedOptionsSetCurrentSettings: (id) sender
755 {
756     /* Set widgets depending on the opt string in field */
757     NSString * thisOpt; // The separated option such as "bframes=3"
758     NSString * optName = @""; // The option name such as "bframes"
759     NSString * optValue = @"";// The option value such as "3"
760     NSArray *currentOptsArray;
761     
762     /*First, we get an opt string to process */
763     NSString *currentOptString = [fDisplayX264Options stringValue];
764     
765     /* Verify there is an opt string to process by making sure an
766        option is getting its value set. If so, start to process it. */
767     NSRange currentOptRange = [currentOptString rangeOfString:@"="];
768     if (currentOptRange.location != NSNotFound)
769     {
770         /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/
771         currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
772         
773         /*iterate through the array and get <opts> and <values*/
774         int loopcounter;
775         int currentOptsArrayCount = [currentOptsArray count];
776         for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
777         {
778             thisOpt = [currentOptsArray objectAtIndex:loopcounter];
779             
780             /* Verify the option sets a value */
781             NSRange splitOptRange = [thisOpt rangeOfString:@"="];            
782             if (splitOptRange.location != NSNotFound)
783             {
784                 /* Split thisOpt into an optName setting an optValue. */
785                 optName = [thisOpt substringToIndex:splitOptRange.location];
786                 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
787                 
788                 /*Run through the available widgets for x264 opts and set them, as you add widgets, 
789                     they need to be added here. This should be moved to its own method probably*/
790                 
791                 /*bframes NSPopUpButton*/
792                 if ([optName isEqualToString:@"bframes"])
793                 {
794                     [fX264optBframesPopUp selectItemAtIndex:[optValue intValue]+1];
795                 }
796                 /*ref NSPopUpButton*/
797                 if ([optName isEqualToString:@"ref"])
798                 {
799                     [fX264optRefPopUp selectItemAtIndex:[optValue intValue]+1];
800                 }
801                 /*No Fast PSkip NSButton*/
802                 if ([optName isEqualToString:@"no-fast-pskip"])
803                 {
804                     [fX264optNfpskipSwitch setState:[optValue intValue]];
805                 }
806                 /*No Dict Decimate NSButton*/
807                 if ([optName isEqualToString:@"no-dct-decimate"])
808                 {
809                     [fX264optNodctdcmtSwitch setState:[optValue intValue]];
810                 }
811                 /*Sub Me NSPopUpButton*/
812                 if ([optName isEqualToString:@"subq"])
813                 {
814                     [fX264optSubmePopUp selectItemAtIndex:[optValue intValue]+1];
815                 }
816                 /*Trellis NSPopUpButton*/
817                 if ([optName isEqualToString:@"trellis"])
818                 {
819                     [fX264optTrellisPopUp selectItemAtIndex:[optValue intValue]+1];
820                 }
821                 /*Mixed Refs NSButton*/
822                 if ([optName isEqualToString:@"mixed-refs"])
823                 {
824                     [fX264optMixedRefsSwitch setState:[optValue intValue]];
825                 }
826                 /*Motion Estimation NSPopUpButton*/
827                 if ([optName isEqualToString:@"me"])
828                 {
829                     if ([optValue isEqualToString:@"dia"])
830                         [fX264optMotionEstPopUp selectItemAtIndex:1];
831                     else if ([optValue isEqualToString:@"hex"])
832                         [fX264optMotionEstPopUp selectItemAtIndex:2];
833                     else if ([optValue isEqualToString:@"umh"])
834                         [fX264optMotionEstPopUp selectItemAtIndex:3];
835                     else if ([optValue isEqualToString:@"esa"])
836                         [fX264optMotionEstPopUp selectItemAtIndex:4];
837                     else if ([optValue isEqualToString:@"tesa"])
838                         [fX264optMotionEstPopUp selectItemAtIndex:5];
839                 }
840                 /*ME Range NSPopUpButton*/
841                 if ([optName isEqualToString:@"merange"])
842                 {
843                     [fX264optMERangePopUp selectItemAtIndex:[optValue intValue]-3];
844                 }
845                 /* Adaptive B-Frames NSPopUpButton*/
846                 if ([optName isEqualToString:@"b-adapt"])
847                 {
848                     [fX264optBAdaptPopUp selectItemAtIndex:[optValue intValue]+1];
849                 }
850                 /*Weighted B-Frames NSButton*/
851                 if ([optName isEqualToString:@"weightb"])
852                 {
853                     [fX264optWeightBSwitch setState:[optValue intValue]];
854                 }
855                 /*B Pyramid NSPButton*/
856                 if ([optName isEqualToString:@"b-pyramid"])
857                 {
858                     [fX264optBPyramidSwitch setState:[optValue intValue]];
859                 }
860                 /*Direct B-frame Prediction NSPopUpButton*/
861                 if ([optName isEqualToString:@"direct"])
862                 {
863                     if ([optValue isEqualToString:@"none"])
864                         [fX264optDirectPredPopUp selectItemAtIndex:1];
865                     else if ([optValue isEqualToString:@"spatial"])
866                         [fX264optDirectPredPopUp selectItemAtIndex:2];
867                     else if ([optValue isEqualToString:@"temporal"])
868                         [fX264optDirectPredPopUp selectItemAtIndex:3];
869                     else if ([optValue isEqualToString:@"auto"])
870                         [fX264optDirectPredPopUp selectItemAtIndex:4];                        
871                 }
872                 /*Deblocking NSPopUpButtons*/
873                 if ([optName isEqualToString:@"deblock"])
874                 {
875                     NSString * alphaDeblock = @"";
876                     NSString * betaDeblock = @"";
877                     
878                     NSRange splitDeblock = [optValue rangeOfString:@","];
879                     alphaDeblock = [optValue substringToIndex:splitDeblock.location];
880                     betaDeblock = [optValue substringFromIndex:splitDeblock.location + 1];
881                     
882                     if ([alphaDeblock isEqualToString:@"0"] && [betaDeblock isEqualToString:@"0"])
883                     {
884                         /* When both filters are at 0, default */
885                         [fX264optAlphaDeblockPopUp selectItemAtIndex:0];                        
886                         [fX264optBetaDeblockPopUp selectItemAtIndex:0];                               
887                     }
888                     else
889                     {
890                         if (![alphaDeblock isEqualToString:@"0"])
891                         {
892                             /* Alpha isn't 0, so set it. The offset of 7 is
893                                because filters start at -6 instead of at 0. */
894                             [fX264optAlphaDeblockPopUp selectItemAtIndex:[alphaDeblock intValue]+7];
895                         }
896                         else
897                         {
898                             /* Set alpha filter to 0, which is 7 up
899                                because filters start at -6, not 0. */
900                             [fX264optAlphaDeblockPopUp selectItemAtIndex:7];                        
901                         }
902                         
903                         if (![betaDeblock isEqualToString:@"0"])
904                         {
905                             /* Beta isn't 0, so set it. */
906                             [fX264optBetaDeblockPopUp selectItemAtIndex:[betaDeblock intValue]+7];
907                         }
908                         else
909                         {
910                             /* Set beta filter to 0. */
911                             [fX264optBetaDeblockPopUp selectItemAtIndex:7];                        
912                         }
913                     }
914                 }
915                 /* Analysis NSPopUpButton */
916                 if ([optName isEqualToString:@"analyse"])
917                 {
918                     if ([optValue isEqualToString:@"p8x8,b8x8,i8x8,i4x4"])
919                     {
920                         /* Default ("some") */
921                         [fX264optAnalysePopUp selectItemAtIndex:0];
922                     }
923                     if ([optValue isEqualToString:@"none"])
924                     {
925                         [fX264optAnalysePopUp selectItemAtIndex:1];
926                     }
927                     if ([optValue isEqualToString:@"all"])
928                     {
929                         [fX264optAnalysePopUp selectItemAtIndex:2];
930                     }
931                 }
932                 /* 8x8 DCT NSButton */
933                 if ([optName isEqualToString:@"8x8dct"])
934                 {
935                     [fX264opt8x8dctSwitch setState:[optValue intValue]];
936                 }
937                 /* CABAC NSButton */
938                 if ([optName isEqualToString:@"cabac"])
939                 {
940                     [fX264optCabacSwitch setState:[optValue intValue]];
941                 }
942                 /* Psy-RD and Psy-Trellis NSSliders */
943                 if ([optName isEqualToString:@"psy-rd"])
944                 {
945                     NSString * rdOpt = @"";
946                     NSString * trellisOpt = @"";
947                     
948                     NSRange splitRD = [optValue rangeOfString:@","];
949                     rdOpt = [optValue substringToIndex:splitRD.location];
950                     trellisOpt = [optValue substringFromIndex:splitRD.location + 1];
951                     
952                     [fX264optPsyRDSlider setFloatValue:[rdOpt floatValue]];
953                     [fX264optPsyTrellisSlider setFloatValue:[trellisOpt floatValue]];
954                 }                                                              
955             }
956         }
957     }
958 }
959
960 - (NSString *) X264AdvancedOptionsOptIDToString: (id) widget
961 {
962     /*Determine which outlet is being used and set optName to process accordingly */
963     NSString * optNameToChange = @""; // The option name such as "bframes"
964     
965     if (widget == fX264optBframesPopUp)
966     {
967         optNameToChange = @"bframes";
968     }
969     if (widget == fX264optRefPopUp)
970     {
971         optNameToChange = @"ref";
972     }
973     if (widget == fX264optNfpskipSwitch)
974     {
975         optNameToChange = @"no-fast-pskip";
976     }
977     if (widget == fX264optNodctdcmtSwitch)
978     {
979         optNameToChange = @"no-dct-decimate";
980     }
981     if (widget == fX264optSubmePopUp)
982     {
983         optNameToChange = @"subq";
984     }
985     if (widget == fX264optTrellisPopUp)
986     {
987         optNameToChange = @"trellis";
988     }
989     if (widget == fX264optMixedRefsSwitch)
990     {
991         optNameToChange = @"mixed-refs";
992     }
993     if (widget == fX264optMotionEstPopUp)
994     {
995         optNameToChange = @"me";
996     }
997     if (widget == fX264optMERangePopUp)
998     {
999         optNameToChange = @"merange";
1000     }
1001     if (widget == fX264optBAdaptPopUp)
1002     {
1003         optNameToChange = @"b-adapt";
1004     }
1005     if (widget == fX264optWeightBSwitch)
1006     {
1007         optNameToChange = @"weightb";
1008     }
1009     if (widget == fX264optBPyramidSwitch)
1010     {
1011         optNameToChange = @"b-pyramid";
1012     }
1013     if (widget == fX264optDirectPredPopUp)
1014     {
1015         optNameToChange = @"direct";
1016     }
1017     if (widget == fX264optAlphaDeblockPopUp)
1018     {
1019         optNameToChange = @"deblock";
1020     }
1021     if (widget == fX264optBetaDeblockPopUp)
1022     {
1023         optNameToChange = @"deblock";
1024     }        
1025     if (widget == fX264optAnalysePopUp)
1026     {
1027         optNameToChange = @"analyse";
1028     }
1029     if (widget == fX264opt8x8dctSwitch)
1030     {
1031         optNameToChange = @"8x8dct";
1032     }
1033     if (widget == fX264optCabacSwitch)
1034     {
1035         optNameToChange = @"cabac";
1036     }
1037     if( widget == fX264optPsyRDSlider)
1038     {
1039         optNameToChange = @"psy-rd";
1040     }
1041     if( widget == fX264optPsyTrellisSlider)
1042     {
1043         optNameToChange = @"psy-rd";
1044     }
1045     
1046     return optNameToChange;
1047 }
1048
1049 - (NSString *) X264AdvancedOptionsWidgetToString: (NSString *) optName withID: (id) sender
1050 {
1051     NSString * thisOpt = @""; // The option=value string the method will return
1052     
1053     if ([optName isEqualToString:@"deblock"])
1054     {
1055         if ((([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 7)) && (([fX264optBetaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optBetaDeblockPopUp indexOfSelectedItem] == 7)))
1056         {
1057             /* When both deblock widgets are 0 or default or a mix of the two,
1058                use a blank string, since deblocking defaults to 0,0.           */
1059             thisOpt = @"";                                
1060         }
1061         else
1062         {
1063             /* Otherwise the format is deblock=a,b, where a and b both have an array
1064                offset of 7 because deblocking values start at -6 instead of at zero. */
1065             thisOpt = [NSString stringWithFormat:@"%@=%d,%d",optName, ([fX264optAlphaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optAlphaDeblockPopUp indexOfSelectedItem]-7 : 0,([fX264optBetaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optBetaDeblockPopUp indexOfSelectedItem]-7 : 0];
1066         }
1067     }
1068     
1069     else if ([optName isEqualToString:@"psy-rd"])
1070     {
1071         if( [fX264optPsyRDSlider floatValue] == 1.0 && [fX264optPsyTrellisSlider floatValue] == 0.0 ) 
1072         {
1073             /* When  PsyRD is 1 and PsyTrel is 0 they're default values and can be ignored. */
1074             thisOpt = @"";                                
1075         }
1076         else
1077         {
1078             /* Otherwise the format is deblock=a,b, where a and b both have an array
1079                offset of 7 because deblocking values start at -6 instead of at zero. */
1080             thisOpt = [NSString stringWithFormat:@"%@=%0.1f,%0.1f", optName, [fX264optPsyRDSlider floatValue], [fX264optPsyTrellisSlider floatValue] ];
1081         }
1082     }
1083     
1084     else if /*Boolean Switches*/ ( [optName isEqualToString:@"b-pyramid"] || [optName isEqualToString:@"no-fast-pskip"] || [optName isEqualToString:@"no-dct-decimate"] )
1085     {
1086         /* Here is where we take care of the boolean options that work overtly:
1087            no-dct-decimate being on means no-dct-decimate=1, etc. Some options
1088            require the inverse, but those will be handled a couple lines down. */
1089         if ([sender state] == 0)
1090         {
1091             /* When these options are false, don't include them. They all default
1092                to being set off, so they don't need to be mentioned at all.       */
1093             thisOpt = @"";
1094         }
1095         else
1096         {
1097             /* Otherwise, include them as optioname=1 */
1098             thisOpt = [NSString stringWithFormat:@"%@=%d",optName,1];
1099         }
1100     }
1101     
1102     else if ( [optName isEqualToString:@"8x8dct"] || [optName isEqualToString:@"weightb"] || [optName isEqualToString:@"mixed-refs"] || [optName isEqualToString:@"cabac"] )
1103     {
1104         /* These options default to being on. That means they
1105            only need to be included in the string when turned off. */
1106         if ([sender state] == 1)
1107         {
1108             /* It's true so don't include it. */
1109             thisOpt = @"";
1110         }
1111         else
1112         {
1113             /* Otherwise, include cabac=0, etc, in the string. */
1114             thisOpt = [NSString stringWithFormat:@"%@=%d",optName,0];
1115         }
1116     }
1117                                             
1118     else if (([sender indexOfSelectedItem] == 0) && (sender != fX264optAlphaDeblockPopUp) && (sender != fX264optBetaDeblockPopUp) ) // means that "unspecified" is chosen, lets then remove it from the string
1119     {
1120         /* When a widget is at index 0, it's default. Default means don't add to the string.
1121            The exception for deblocking is because for those, *both* need to at index 0
1122            for it to default, so it's handled separately, above this section.                */
1123         thisOpt = @"";
1124     }
1125     
1126     else if ([optName isEqualToString:@"me"])
1127     {
1128         /* Motion estimation uses string values, so this switch
1129            pairs the widget index with the right value string.  */
1130         switch ([sender indexOfSelectedItem])
1131         {   
1132             case 1:
1133                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"dia"];
1134                 break;
1135                 
1136             case 2:
1137                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"hex"];
1138                 break;
1139                 
1140             case 3:
1141                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"umh"];
1142                 break;
1143                 
1144             case 4:
1145                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"esa"];
1146                 break;
1147             
1148             case 5:
1149                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"tesa"];
1150             
1151             default:
1152                 break;
1153         }
1154     }
1155     
1156     else if ([optName isEqualToString:@"direct"])
1157     {
1158         /* Direct prediction uses string values, so this switch
1159            pairs the right string value with the right widget index. */
1160         switch ([sender indexOfSelectedItem])
1161         {   
1162             case 1:
1163                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
1164                 break;
1165                 
1166             case 2:
1167                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"spatial"];
1168                 break;
1169                 
1170             case 3:
1171                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"temporal"];
1172                 break;
1173                 
1174             case 4:
1175                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"auto"];
1176                 break;
1177                 
1178             default:
1179                 break;
1180         }
1181     }
1182     
1183     else if ([optName isEqualToString:@"analyse"])
1184     {
1185         /* Analysis uses string values as well. */
1186         switch ([sender indexOfSelectedItem])
1187         {   
1188             case 1:
1189                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
1190                 break;
1191                 
1192             case 2:
1193                 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"all"];
1194                 break;
1195                 
1196             default:
1197                 break;
1198         }
1199     }
1200     
1201     else if ([optName isEqualToString:@"merange"])
1202     {
1203         /* Motion estimation range uses an odd array offset because in addition
1204            to starting with index 0 as default, index 1 starts at 4 instead of 1,
1205            because merange can't go below 4. So it has to be handled separately.  */
1206         thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]+3];
1207     }
1208     
1209     else if ([optName isEqualToString:@"b-adapt"])
1210     {
1211         /* B-adapt starts at index 0 with default then goes 0, 1, 2)*/
1212         thisOpt = [NSString stringWithFormat:@"%@=%d", optName, [sender indexOfSelectedItem]-1];
1213     }
1214     
1215     else // we have a valid value to change, so change it
1216     {
1217         if ( [sender indexOfSelectedItem] != 0 )
1218         /* Here's our general case, that catches things like ref frames and b-frames.
1219            Basically, any options that are PopUp menus with index 0 as default and
1220            index 1 as 1, with numerical values, are all handled right here. All of
1221            the above stuff is for the exceptions to the general case.              */
1222             thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]-1];
1223     }
1224     
1225     return thisOpt;
1226 }
1227
1228 - (BOOL) X264AdvancedOptionsIsOpt: (NSString *) optNameToChange inString: (NSString *) currentOptString
1229 {
1230     /* If the option is in the string but not the beginning of it,
1231        it will be in the form of ":optName=value" so we really want
1232        to be looking for ":optNameToChange=" rather than "optNameToChange". */
1233     NSString *checkOptNameToChange = [NSString stringWithFormat:@":%@=",optNameToChange];
1234     
1235     /* Now we store the part of the string up through the option name in currentOptRange. */
1236     NSRange currentOptRange = [currentOptString rangeOfString:checkOptNameToChange];
1237
1238     /*  We need to know if the option is at the beginning of the string.
1239         If it is at the start, it won't be preceded by a colon.
1240         To figure this out, we'll use the rangeOfString method. First,
1241         store what the option name would be if if it was at the beginning,
1242         in checkOptNameToChangeBeginning. Then, find its range in the string.
1243         If the range is 0, it's the first option listed in the string.       */        
1244     NSString *checkOptNameToChangeBeginning = [NSString stringWithFormat:@"%@=",optNameToChange];
1245     NSRange currentOptRangeBeginning = [currentOptString rangeOfString:checkOptNameToChangeBeginning];
1246
1247     if (currentOptRange.location != NSNotFound || currentOptRangeBeginning.location == 0)
1248         return true;
1249     else
1250         return false;
1251
1252
1253 /**
1254  * Resets the option string to mirror the GUI widgets.
1255  */
1256 - (IBAction) X264AdvancedOptionsChanged: (id) sender
1257 {
1258     /* Look up the equivalent string option name of the calling widget. */
1259     NSString * optNameToChange = [self X264AdvancedOptionsOptIDToString: sender];
1260     
1261     NSString * thisOpt = @"";  // The separated option such as "bframes=3"
1262     NSString * optName = @"";  // The option name such as "bframes"
1263     NSString * optValue = @""; // The option value such as "3"
1264     NSArray *currentOptsArray;
1265     
1266     /* Get the current opt string being displayed. */
1267     NSString *currentOptString = [fDisplayX264Options stringValue];
1268     
1269     /* There are going to be a few possibilities.
1270        - The option might start off the string.
1271        - The option might be in the middle of the string.
1272        - The option might not be in the string at all yet.
1273        - The string itself might not yet exist.             */
1274     
1275     if( [self X264AdvancedOptionsIsOpt: optNameToChange inString: currentOptString] )
1276     {
1277         /* If the option is in the string wth a semicolon, or starts the string, it's time to edit.
1278            This means parsing the whole string into an array of options and values. From there,
1279            iterate through the options, and when you reach the one that's been changed, edit it.   */
1280         
1281         /* Create new empty opt string*/
1282         NSString *changedOptString = @"";
1283         
1284         /* Put individual options into an array based on the ":"
1285            separator for processing, result is "<opt>=<value>"   */
1286         currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
1287         
1288         /* Iterate through the array and get <opts> and <values*/
1289         int loopcounter;
1290         int currentOptsArrayCount = [currentOptsArray count];
1291         for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
1292         {
1293             thisOpt = [currentOptsArray objectAtIndex:loopcounter];
1294             NSRange splitOptRange = [thisOpt rangeOfString:@"="];
1295             
1296             if (splitOptRange.location != NSNotFound)
1297             {
1298                 /* First off, it's time to handle option strings that
1299                    already have at least one option=value pair in them. */
1300                    
1301                 optName = [thisOpt substringToIndex:splitOptRange.location];
1302                 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
1303
1304                 /*If the optNameToChange is found, appropriately change the value or delete it if
1305                     "Unspecified" is set.*/
1306                 if ([optName isEqualToString:optNameToChange])
1307                 {
1308                     thisOpt = [self X264AdvancedOptionsWidgetToString: optName withID: sender];
1309                 }
1310             }
1311             
1312             /* Construct New String for opts here */
1313             if ([thisOpt isEqualToString:@""])
1314             {
1315                 /* Blank option, so just add it to the string. (Why?) */
1316                 changedOptString = [NSString stringWithFormat:@"%@%@",changedOptString,thisOpt];
1317             }
1318             else
1319             {
1320                 if ([changedOptString isEqualToString:@""])
1321                 {
1322                     /* No existing string, make the string this option. */
1323                     changedOptString = [NSString stringWithFormat:@"%@",thisOpt];
1324                 }
1325                 else
1326                 {
1327                     /* Existing string, existing option. Append the
1328                        option to the string, preceding it with a colon. */
1329                     changedOptString = [NSString stringWithFormat:@"%@:%@",changedOptString,thisOpt];
1330                 }
1331             }
1332         }
1333         
1334         /* Change the dislayed option string to reflect the new modified settings */
1335         [fDisplayX264Options setStringValue:changedOptString];    
1336     }
1337     else // if none exists, add it to the string
1338     {
1339         /* This is where options that aren't already in the string are handled. */
1340         if ([[fDisplayX264Options stringValue] isEqualToString: @""])
1341         {
1342             
1343             [fDisplayX264Options setStringValue:
1344                 [self X264AdvancedOptionsWidgetToString: optNameToChange withID: sender]];
1345         }
1346         else
1347         {
1348             /* The string isn't empty, and the option isn't already in it, so
1349                it will need to be appended to the current string with a colon,
1350                as long as the string to be appended isn't just blank (default). */
1351             if( [[self X264AdvancedOptionsWidgetToString: optNameToChange withID: sender] isEqualToString: @""] == false )
1352             {
1353                 [fDisplayX264Options setStringValue:
1354                     [NSString stringWithFormat:@"%@:%@",
1355                         currentOptString,
1356                         [self X264AdvancedOptionsWidgetToString: optNameToChange withID: sender] ]];                
1357             }
1358         }
1359     }
1360     
1361     /* We now need to reset the opt widgets since we changed some stuff */        
1362     [self X264AdvancedOptionsSet:sender];        
1363 }
1364
1365 @end