OSDN Git Service

MacGui: Remove Target Size as a rate control option as it doesn't really work correct...
[handbrake-jp/handbrake-jp-git.git] / macosx / HBAdvancedController.m
index b1c269f..c8c8b12 100644 (file)
     unsigned i;
     NSControl * controls[] =
       { fX264optViewTitleLabel,fDisplayX264Options,fDisplayX264OptionsLabel,fX264optBframesLabel,
-        fX264optBframesPopUp,fX264optRefLabel,fX264optRefPopUp,fX264optNfpskipLabel,fX264optNfpskipSwitch,
+        fX264optBframesPopUp,fX264optRefLabel,fX264optRefPopUp,
         fX264optNodctdcmtLabel,fX264optNodctdcmtSwitch,fX264optSubmeLabel,fX264optSubmePopUp,
-        fX264optTrellisLabel,fX264optTrellisPopUp,fX264optMixedRefsLabel,fX264optMixedRefsSwitch,
+        fX264optTrellisLabel,fX264optTrellisPopUp, fX264optWeightPLabel, fX264optWeightPSwitch,
         fX264optMotionEstLabel,fX264optMotionEstPopUp,fX264optMERangeLabel,fX264optMERangePopUp,
-        fX264optWeightBLabel,fX264optWeightBSwitch, fX264optBPyramidLabel,fX264optBPyramidSwitch,
+        fX264optBPyramidLabel,fX264optBPyramidPopUp, fX264optAqLabel, fX264optAqSlider,
         fX264optDirectPredLabel,fX264optDirectPredPopUp,fX264optDeblockLabel,fX264optAnalyseLabel,
         fX264optAnalysePopUp,fX264opt8x8dctLabel,fX264opt8x8dctSwitch,fX264optCabacLabel,fX264optCabacSwitch,
-        fX264optAlphaDeblockPopUp,fX264optBetaDeblockPopUp, fX264optPsyRDSlider, fX264optPsyRDLabel, fX264optPsyTrellisSlider, fX264optPsyTrellisLabel };
+        fX264optAlphaDeblockPopUp,fX264optBetaDeblockPopUp, fX264optPsyRDSlider, fX264optPsyRDLabel, fX264optPsyTrellisSlider, fX264optPsyTrellisLabel, fX264optBAdaptPopUp, fX264optBAdaptLabel };
 
     for( i = 0; i < sizeof( controls ) / sizeof( NSControl * ); i++ )
     {
 {
     /*Set opt widget values here*/
     
+    NSString * toolTip = @"";
+    
     /*B-Frames fX264optBframesPopUp*/
     int i;
     [fX264optBframesPopUp removeAllItems];
-    [fX264optBframesPopUp addItemWithTitle:@"Default (0)"];
+    [fX264optBframesPopUp addItemWithTitle:@"Default (3)"];
     for (i=0; i<17;i++)
     {
         [fX264optBframesPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
     }
+    toolTip =
+        @"Sane values are ~2-5.  This specifies the maximum number of sequential B-frames that the encoder can use.  Large numbers generally won't help significantly unless Adaptive B-frames is set to Optimal.  Cel-animated source material and B-pyramid also significantly increase the usefulness of larger values. Baseline profile, as required for iPods and similar devices, requires B-frames to be set to 0 (off).";
+    [fX264optBframesPopUp setToolTip: toolTip];
+    [fX264optBframesLabel setToolTip: toolTip];
     
     /*Reference Frames fX264optRefPopUp*/
     [fX264optRefPopUp removeAllItems];
-    [fX264optRefPopUp addItemWithTitle:@"Default (1)"];
-    for (i=0; i<17;i++)
+    [fX264optRefPopUp addItemWithTitle:@"Default (3)"];
+    for (i=1; i<17;i++)
     {
         [fX264optRefPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
     }
-    
-    /*No Fast P-Skip fX264optNfpskipSwitch BOOLEAN*/
-    [fX264optNfpskipSwitch setState:0];
+    toolTip =
+        @"Sane values are ~1-6.  The more you add, the better the compression, but the slower the encode.  Cel animation tends to benefit from more reference frames a lot more than film content.  Note that many hardware devices have limitations on the number of supported reference frames, so if you're encoding for a handheld or standalone player, don't touch this unless you're absolutely sure you know what you're doing!";
+    [fX264optRefPopUp setToolTip: toolTip];
+    [fX264optRefLabel setToolTip: toolTip];
+
+    /*Weight-P fX264optWeightPSwitch BOOLEAN*/
+    [fX264optWeightPSwitch setState:1];
+    toolTip = 
+        @"Performs extra analysis to decide upon weighting parameters for each frame.  This improves overall compression slightly and improves the quality of fades greatly. Baseline profile, as required for iPods and similar devices, requires weighted P-frame prediction to be disabled.  Note that some devices and players, even those that support Main Profile, may have problems with Weighted P-frame prediction: the Apple TV is completely incompatible with it, for example.";
+    [fX264optWeightPSwitch setToolTip: toolTip];
+    [fX264optWeightPLabel setToolTip: toolTip];
     
     /*No Dict Decimate fX264optNodctdcmtSwitch BOOLEAN*/
     [fX264optNodctdcmtSwitch setState:0];    
+    toolTip =
+        @"x264 normally zeroes out nearly-empty data blocks to save bits to be better used for some other purpose in the video.  However, this can sometimes have slight negative effects on retention of subtle grain and dither.  Don't touch this unless you're having banding issues or other such cases where you are having trouble keeping fine noise.";
+    [fX264optNodctdcmtSwitch setToolTip: toolTip];
+    [fX264optNodctdcmtLabel setToolTip: toolTip];
     
     /*Sub Me fX264optSubmePopUp*/
     [fX264optSubmePopUp removeAllItems];
-    [fX264optSubmePopUp addItemWithTitle:@"Default (6)"];
-    for (i=0; i<10;i++)
-    {
-        [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
-    }
+    [fX264optSubmePopUp addItemWithTitle:@"Default (7)"];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"0: SAD, no subpel (super fast!)"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"1: SAD, qpel"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"2: SATD, qpel"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"3: SATD, multi-qpel"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"4: SATD, qpel on all"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"5: SATD, multi-qpel on all"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"6: RD in I/P-frames"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"7: RD in all frames"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"8: RD refine in I/P-frames"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"9: RD refine in all frames"]];
+    [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"10: QPRD in all frames"]];
+    toolTip =
+        @"This setting controls both subpixel-precision motion estimation and mode decision methods.\n\nSubpixel motion estimation is used for refining motion estimates beyond mere pixel accuracy, improving compression.\n\nMode decision is the method used to choose how to encode each block of the frame: a very important decision.\n\nSAD is the fastest method, followed by SATD, RD, RD refinement, and the slowest, QPRD.\n\n6 or higher is strongly recommended: Psy-RD, a very powerful psy optimization that helps retain detail, requires RD.\n\n10, the most powerful and slowest option, requires trellis=2.";
+    [fX264optSubmePopUp setToolTip: toolTip];
+    [fX264optSubmeLabel setToolTip: toolTip];
     
     /*Trellis fX264optTrellisPopUp*/
     [fX264optTrellisPopUp removeAllItems];
-    [fX264optTrellisPopUp addItemWithTitle:@"Default (0)"];
-    for (i=0; i<3;i++)
-    {
-        [fX264optTrellisPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
-    }
+    [fX264optTrellisPopUp addItemWithTitle:@"Default (Encode only)"];
+    [fX264optTrellisPopUp addItemWithTitle:[NSString stringWithFormat:@"Off"]];
+    [fX264optTrellisPopUp addItemWithTitle:[NSString stringWithFormat:@"Encode only"]];
+    [fX264optTrellisPopUp addItemWithTitle:[NSString stringWithFormat:@"Always"]];
     [fX264optTrellisPopUp setWantsLayer:YES];
-    
-    /*Mixed-references fX264optMixedRefsSwitch BOOLEAN*/
-    [fX264optMixedRefsSwitch setState:0];
-    [fX264optMixedRefsSwitch setWantsLayer:YES];
+    toolTip =
+        @"Trellis fine-tunes the rounding of transform coefficients to squeeze out 3-5% more compression at the cost of some speed. \"Always\" uses trellis not only during the main encoding process, but also during analysis, which improves compression even more, albeit at great speed cost. Trellis costs more speed at higher bitrates.";
+    [fX264optTrellisPopUp setToolTip: toolTip];
+    [fX264optTrellisLabel setToolTip: toolTip];
     
     /*Motion Estimation fX264optMotionEstPopUp*/
     [fX264optMotionEstPopUp removeAllItems];
     [fX264optMotionEstPopUp addItemWithTitle:@"Uneven Multi-Hexagon"];
     [fX264optMotionEstPopUp addItemWithTitle:@"Exhaustive"];
     [fX264optMotionEstPopUp addItemWithTitle:@"Transformed Exhaustive"];
+    toolTip =
+        @"Controls the motion estimation method. Motion estimation is how the encoder estimates how each block of pixels in a frame has moved.  A better motion search method improves compression at the cost of speed.\n\nDiamond: performs an extremely fast and simple search using a diamond pattern.\n\nHexagon: performs a somewhat more effective but slightly slower search using a hexagon pattern.\n\nUneven Multi-Hex: performs a very wide search using a variety of patterns, more accurately capturing complex motion.\n\nExhaustive: performs a \"dumb\" search of every pixel in a wide area.  Significantly slower for only a small compression gain.\n\nTransformed Exhaustive: Like exhaustive, but makes even more accurate decisions. Accordingly, somewhat slower, also for only a small improvement.";
+    [fX264optMotionEstPopUp setToolTip: toolTip];
+    [fX264optMotionEstLabel setToolTip: toolTip];
     
     /*Motion Estimation range fX264optMERangePopUp*/
     [fX264optMERangePopUp removeAllItems];
     {
         [fX264optMERangePopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
     }
+    toolTip =
+        @"This is the distance x264 searches from its best guess at the motion of a block in order to try to find its actual motion.  Doesn't apply to Diamond or Hexagon search options.  The default is fine for most content, but extremely high motion video, especially at HD resolutions, may benefit from higher ranges, albeit at a high speed cost.";
+    [fX264optMERangePopUp setToolTip: toolTip];
+    [fX264optMERangeLabel setToolTip: toolTip];
     
-    /*Weighted B-Frame Prediction fX264optWeightBSwitch BOOLEAN*/
-    [fX264optWeightBSwitch setState:0];
-    [fX264optWeightBSwitch setWantsLayer:YES];
-    
-    /*B-frame Pyramids fX264optBPyramidSwitch BOOLEAN*/
-    [fX264optBPyramidSwitch setState:0];
-    [fX264optBPyramidSwitch setWantsLayer:YES];
+    /*B-frame Pyramids fX264optBPyramidPopUp*/
+    [fX264optBPyramidPopUp removeAllItems];
+    [fX264optBPyramidPopUp addItemWithTitle:@"Default (Normal)"];
+    [fX264optBPyramidPopUp addItemWithTitle:@"Off"];
+    [fX264optBPyramidPopUp addItemWithTitle:@"Strict"];
+    [fX264optBPyramidPopUp setWantsLayer:YES];
+    toolTip =
+        @"B-pyramid improves compression by creating a pyramidal structure (hence the name) of B-frames, allowing B-frames to reference each other to improve compression.  Requires Max B-frames greater than 1; optimal adaptive B-frames is strongly recommended for full compression benefit.";
+    [fX264optBPyramidPopUp setToolTip: toolTip];
+    [fX264optBPyramidLabel setToolTip: toolTip];
     
     /*Direct B-Frame Prediction Mode fX264optDirectPredPopUp*/
     [fX264optDirectPredPopUp removeAllItems];
     [fX264optDirectPredPopUp addItemWithTitle:@"Temporal"];
     [fX264optDirectPredPopUp addItemWithTitle:@"Automatic"];
     [fX264optDirectPredPopUp setWantsLayer:YES];
+    toolTip =
+        @"H.264 allows for two different prediction modes, spatial and temporal, in B-frames.\n\nSpatial, the default, is almost always better, but temporal is sometimes useful too.\n\nx264 can, at the cost of a small amount of speed (and accordingly for a small compression gain), adaptively select which is better for each particular frame.";
+    [fX264optDirectPredPopUp setToolTip: toolTip];
+    [fX264optDirectPredLabel setToolTip: toolTip];
+    
+    /* Adaptive B-Frames Mode fX264optBAdaptPopUp */
+    [fX264optBAdaptPopUp removeAllItems];
+    [fX264optBAdaptPopUp addItemWithTitle:@"Default (Fast)"];
+    [fX264optBAdaptPopUp addItemWithTitle:@"Off"];
+    [fX264optBAdaptPopUp addItemWithTitle:@"Fast"];
+    [fX264optBAdaptPopUp addItemWithTitle:@"Optimal"];
+    [fX264optBAdaptPopUp setWantsLayer:YES];
+    toolTip =
+        @"x264 has a variety of algorithms to decide when to use B-frames and how many to use.\n\nFast mode takes roughly the same amount of time no matter how many B-frames you specify.  However, while fast, its decisions are often suboptimal.\n\nOptimal mode gets slower as the maximum number of B-Frames increases, but makes much more accurate decisions, especially when used with B-pyramid.";
+    [fX264optBAdaptPopUp setToolTip: toolTip];
+    [fX264optBAdaptLabel setToolTip: toolTip];
     
     /*Alpha Deblock*/
     [fX264optAlphaDeblockPopUp removeAllItems];
     {
         [fX264optAlphaDeblockPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
     }
+    toolTip =
+        @"H.264 has a built-in deblocking filter that smooths out blocking artifacts after decoding each frame.  This not only improves visual quality, but also helps compression significantly. The deblocking filter takes a lot of CPU power, so if you're looking to minimize CPU requirements for video playback, disable it.\n\nThe deblocking filter has two adjustable parameters, \"strength\" and \"threshold\". The former controls how strong (or weak) the deblocker is, while the latter controls how many (or few) edges it applies to. Lower values mean less deblocking, higher values mean more deblocking. The default is 0 (normal strength) for both parameters.";
+    [fX264optAlphaDeblockPopUp setToolTip: toolTip];
+    [fX264optDeblockLabel setToolTip: toolTip];
 
     /*Beta Deblock*/
     [fX264optBetaDeblockPopUp removeAllItems];
     {
         [fX264optBetaDeblockPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
     }
+    [fX264optBetaDeblockPopUp setToolTip: toolTip];
+    [fX264optDeblockLabel setToolTip: toolTip];
 
     /* Analysis fX264optAnalysePopUp */
     [fX264optAnalysePopUp removeAllItems];
-    [fX264optAnalysePopUp addItemWithTitle:@"Default (some)"]; /* 0=default */
+    [fX264optAnalysePopUp addItemWithTitle:@"Default (Most)"]; /* 0=default */
     [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"None"]]; /* 1=none */
-    [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"All"]]; /* 2=all */
+    [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"Some"]]; /* 2=some */
+    [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"All"]]; /* 3=all */
+    toolTip =
+        @"Mode decision picks from a variety of options to make its decision: this option chooses what options those are.  Fewer partitions to check means faster encoding, at the cost of worse decisions, since the best option might have been one that was turned off.";
+    [fX264optAnalysePopUp setToolTip: toolTip];
+    [fX264optAnalyseLabel setToolTip: toolTip];
 
     /* 8x8 DCT fX264op8x8dctSwitch */
-    [fX264opt8x8dctSwitch setState:0];
+    [fX264opt8x8dctSwitch setState:1];
     [fX264opt8x8dctSwitch setWantsLayer:YES];
+    toolTip =
+        @"The 8x8 transform is the single most useful feature of x264 in terms of compression-per-speed.  It improves compression by at least 5% at a very small speed cost and may provide an unusually high visual quality benefit compared to its compression gain.  However, it requires High Profile, which many devices may not support.";
+    [fX264opt8x8dctSwitch setToolTip: toolTip];
+    [fX264opt8x8dctLabel setToolTip: toolTip];
 
     /* CABAC fX264opCabacSwitch */
     [fX264optCabacSwitch setState:1];
+    toolTip =
+        @"After the encoder has done its work, it has a bunch of data that needs to be compressed losslessly, similar to ZIP or RAR.  H.264 provides two options for this: CAVLC and CABAC.  CABAC decodes a lot slower but compresses significantly better (10-30%), especially at lower bitrates.  If you're looking to minimize CPU requirements for video playback, disable this option. Baseline profile, as required for iPods and similar devices, requires CABAC to be disabled.";
+    [fX264optCabacSwitch setToolTip: toolTip];
+    [fX264optCabacLabel setToolTip: toolTip];
+
+    /* Adaptive Quantization Strength fX264opAqSlider */
+    [fX264optAqSlider setMinValue:0.0];
+    [fX264optAqSlider setMaxValue:2.0];
+    [fX264optAqSlider setTickMarkPosition:NSTickMarkBelow];
+    [fX264optAqSlider setNumberOfTickMarks:21];
+    [fX264optAqSlider setAllowsTickMarkValuesOnly:YES];
+    [fX264optAqSlider setFloatValue:1.0];
+    toolTip =
+        @"Adaptive quantization controls how the encoder distributes bits across the frame.  Higher values take more bits away from edges and complex areas to improve areas with finer detail.";
+    [fX264optAqSlider setToolTip: toolTip];
+    [fX264optAqLabel setToolTip: toolTip];
     
     /* PsyRDO fX264optPsyRDSlider */
     [fX264optPsyRDSlider setMinValue:0.0];
-    [fX264optPsyRDSlider setMaxValue:1.0];
+    [fX264optPsyRDSlider setMaxValue:2.0];
     [fX264optPsyRDSlider setTickMarkPosition:NSTickMarkBelow];
-    [fX264optPsyRDSlider setNumberOfTickMarks:10];
+    [fX264optPsyRDSlider setNumberOfTickMarks:21];
     [fX264optPsyRDSlider setAllowsTickMarkValuesOnly:YES];
     [fX264optPsyRDSlider setFloatValue:1.0];
+    toolTip =
+        @"Psychovisual rate-distortion optimization takes advantage of the characteristics of human vision to dramatically improve apparent detail and sharpness.  The effect can be made weaker or stronger by adjusting the strength.  Being an RD algorithm, it requires mode decision to be at least \"6\".";
+    [fX264optPsyRDSlider setToolTip: toolTip];
+    [fX264optPsyRDLabel setToolTip: toolTip];
 
     /* PsyTrellis fX264optPsyRDSlider */
     [fX264optPsyTrellisSlider setMinValue:0.0];
     [fX264optPsyTrellisSlider setMaxValue:1.0];
     [fX264optPsyTrellisSlider setTickMarkPosition:NSTickMarkBelow];
-    [fX264optPsyTrellisSlider setNumberOfTickMarks:10];
+    [fX264optPsyTrellisSlider setNumberOfTickMarks:21];
     [fX264optPsyTrellisSlider setAllowsTickMarkValuesOnly:YES];
     [fX264optPsyTrellisSlider setFloatValue:0.0];
+    toolTip =
+        @"Psychovisual trellis is an experimental algorithm to further improve sharpness and detail retention beyond what Psychovisual RD does.  Recommended values are around 0.2, though higher values may help for very grainy video or lower bitrate encodes.  Not recommended for cel animation and other sharp-edged graphics.";
+    [fX264optPsyTrellisSlider setToolTip: toolTip];
+    [fX264optPsyTrellisLabel setToolTip: toolTip];
 
     /* Standardize the option string */
     [self X264AdvancedOptionsStandardizeOptString:nil];
 
     /* Set Current GUI Settings based on newly standardized string */
-    [self X264AdvancedOptionsSetCurrentSettings:nil];
+    [self X264AdvancedOptionsSetCurrentSettings:sender];
 
     /* Fade out options that don't apply */
     [self X264AdvancedOptionsAnimate: sender];
     }
     
     /* Change the option string to reflect the new standardized option string */
-    [fDisplayX264Options setStringValue:[NSString stringWithFormat:changedOptString]];
+    [fDisplayX264Options setStringValue:changedOptString];
 }
 
 /**
         cleanOptNameString = @"ref";
     }
     
-    /*No Fast PSkip nofast_pskip*/
-    if ([cleanOptNameString isEqualToString:@"no-fast-pskip"] || [cleanOptNameString isEqualToString:@"no_fast_pskip"] || [cleanOptNameString isEqualToString:@"nofast_pskip"])
-    {
-        cleanOptNameString = @"no-fast-pskip";
-    }
-    
     /*No Dict Decimate*/
     if ([cleanOptNameString isEqualToString:@"no-dct-decimate"] || [cleanOptNameString isEqualToString:@"no_dct_decimate"] || [cleanOptNameString isEqualToString:@"nodct_decimate"])
     {
     if ([cleanOptNameString isEqualToString:@"me-range"] || [cleanOptNameString isEqualToString:@"me_range"])
         cleanOptNameString = @"merange";
     
-    /*WeightB*/
-    if ([cleanOptNameString isEqualToString:@"weight-b"] || [cleanOptNameString isEqualToString:@"weight_b"])
-    {
-        cleanOptNameString = @"weightb";
-    }
-    
     /*B Pyramid*/
     if ([cleanOptNameString isEqualToString:@"b_pyramid"])
     {
 {
     /* Lots of situations to cover.
        - B-frames (when 0 turn of b-frame specific stuff, when < 2 disable b-pyramid)
-       - CABAC (when 0 turn off trellis)
-       - analysis (if none, turn off 8x8dct)
-       - refs (under 2, disable mixed-refs)
+       - CABAC (when 0 turn off trellis and psy-trel)
        - subme (if under 6, turn off psy-rd and psy-trel)
        - trellis (if 0, turn off psy-trel)
     */
     
-    if ( [fX264optBframesPopUp indexOfSelectedItem ] < 2)
+    if( sender == fX264optBframesPopUp || sender == nil || sender == fDisplayX264Options )
     {
-        /* If the b-frame widget is at 0 or 1, the user has chosen
-           not to use b-frames at all. So disable the options
-           that can only be used when b-frames are enabled.        */
-        [[fX264optWeightBSwitch animator] setHidden:YES];
-        [[fX264optWeightBLabel animator] setHidden:YES];
-        if ( [fX264optWeightBSwitch state] == 1 && sender != fX264optWeightBSwitch && sender != fX264optBPyramidSwitch && sender != fX264optDirectPredPopUp)
-            [fX264optWeightBSwitch performClick:self];
-        
-        [[fX264optBPyramidSwitch animator] setHidden:YES];
-        [[fX264optBPyramidLabel animator] setHidden:YES];
-        if ( [fX264optBPyramidSwitch state] == 1 && sender != fX264optWeightBSwitch && sender != fX264optBPyramidSwitch && sender != fX264optDirectPredPopUp)
-            [fX264optBPyramidSwitch performClick:self];
-
-        [[fX264optDirectPredPopUp animator] setHidden:YES];
-        [[fX264optDirectPredLabel animator] setHidden:YES];
-        if ( [fX264optDirectPredPopUp indexOfSelectedItem] > 0 && sender != fX264optWeightBSwitch && sender != fX264optBPyramidSwitch && sender != fX264optDirectPredPopUp)
+        if ( [fX264optBframesPopUp indexOfSelectedItem ] == 1 )
         {
-            [fX264optDirectPredPopUp selectItemAtIndex: 0];
-            [[fX264optDirectPredPopUp cell] performClick:self];
+            /* If the b-frame widget is at 1, the user has chosen
+               not to use b-frames at all. So disable the options
+               that can only be used when b-frames are enabled.        */
             
-        }
-    }
-    else if ( [fX264optBframesPopUp indexOfSelectedItem ] == 2)
-    {
-        /* Only 1 b-frame? Disable b-pyramid. */
-        [[fX264optBPyramidSwitch animator] setHidden:YES];
-        [[fX264optBPyramidLabel animator] setHidden:YES];
-        if ( [fX264optBPyramidSwitch state] == 1 && sender != fX264optBPyramidSwitch)
-            [fX264optBPyramidSwitch performClick:self];
-
-        [[fX264optWeightBSwitch animator] setHidden:NO];
-        [[fX264optWeightBLabel animator] setHidden:NO];
-
-        [[fX264optDirectPredPopUp animator] setHidden:NO];
-        [[fX264optDirectPredLabel animator] setHidden:NO];
-    }
-    else
-    {
-        [[fX264optWeightBSwitch animator] setHidden:NO];
-        [[fX264optWeightBLabel animator] setHidden:NO];
+            if( [fX264optBPyramidPopUp isHidden] == false )
+            {
+                [[fX264optBPyramidPopUp animator] setHidden:YES];
+                [[fX264optBPyramidLabel animator] setHidden:YES];
+                if ( [fX264optBPyramidPopUp indexOfSelectedItem] > 0 )
+                {
+                    [fX264optBPyramidPopUp selectItemAtIndex: 0];
+                    [[fX264optBPyramidPopUp cell] performClick:self];
+                }
+            }
 
-        [[fX264optBPyramidSwitch animator] setHidden:NO];
-        [[fX264optBPyramidLabel animator] setHidden:NO];
+            if( [fX264optDirectPredPopUp isHidden] == false )
+            {
+                [[fX264optDirectPredPopUp animator] setHidden:YES];
+                [[fX264optDirectPredLabel animator] setHidden:YES];
+                if ( [fX264optDirectPredPopUp indexOfSelectedItem] > 0 )
+                {
+                    [fX264optDirectPredPopUp selectItemAtIndex: 0];
+                    [[fX264optDirectPredPopUp cell] performClick:self];
+                }
+            }
 
-        [[fX264optDirectPredPopUp animator] setHidden:NO];
-        [[fX264optDirectPredLabel animator] setHidden:NO];
-    }
-    
-    if ( [fX264optCabacSwitch state] == false)
-    {
-        if( [fX264optTrellisPopUp isHidden] == false )
+            if( [fX264optBAdaptPopUp isHidden] == false )
+            {
+                [[fX264optBAdaptPopUp animator] setHidden:YES];
+                [[fX264optBAdaptLabel animator] setHidden:YES];
+                if ( [fX264optBAdaptPopUp indexOfSelectedItem] > 0 )
+                {
+                    [fX264optBAdaptPopUp selectItemAtIndex: 0];
+                    [[fX264optBAdaptPopUp cell] performClick:self];
+                }
+            }
+        }
+        else if ( [fX264optBframesPopUp indexOfSelectedItem ] == 2)
         {
-            /* Without CABAC entropy coding, trellis doesn't run. */
-            [[fX264optTrellisPopUp animator] setHidden:YES];
-            [[fX264optTrellisLabel animator] setHidden:YES];
-            [fX264optTrellisPopUp selectItemAtIndex:0];
-            if ( (sender != fX264optTrellisPopUp) && (sender != fX264optPsyTrellisSlider) )
+            /* Only 1 b-frame? Disable b-pyramid. */
+            if( [fX264optBPyramidPopUp isHidden] == false )
+            {
+                [[fX264optBPyramidPopUp animator] setHidden:YES];
+                [[fX264optBPyramidLabel animator] setHidden:YES];
+                if ( [fX264optBPyramidPopUp indexOfSelectedItem] > 0 )
+                {
+                    [fX264optBPyramidPopUp selectItemAtIndex: 0];
+                    [[fX264optBPyramidPopUp cell] performClick:self];
+                }
+            }
+
+            if( [fX264optDirectPredPopUp isHidden] == true )
+            {
+                [[fX264optDirectPredPopUp animator] setHidden:NO];
+                [[fX264optDirectPredLabel animator] setHidden:NO];
+            }
+            
+            if( [fX264optBAdaptPopUp isHidden] == true )
             {
-                [[fX264optTrellisPopUp cell] performClick:self];
+                [[fX264optBAdaptPopUp animator] setHidden:NO];
+                [[fX264optBAdaptLabel animator] setHidden:NO];
             }
         }
-    }
-    else if( [fX264optTrellisPopUp isHidden] == true)
-    {
-        [[fX264optTrellisPopUp animator] setHidden:NO];
-        [[fX264optTrellisLabel animator] setHidden:NO];
-    }
-    
-    if ( [fX264optAnalysePopUp indexOfSelectedItem] == 1)
-    {
-        /* No analysis? Disable 8x8dct */
-        [[fX264opt8x8dctSwitch animator] setHidden:YES];
-        [[fX264opt8x8dctLabel animator] setHidden:YES];
-        if ( [fX264opt8x8dctSwitch state] == 1 && sender != fX264opt8x8dctSwitch )
-            [fX264opt8x8dctSwitch performClick:self];
-    }
-    else
-    {
-        [[fX264opt8x8dctSwitch animator] setHidden:NO];
-        [[fX264opt8x8dctLabel animator] setHidden:NO];
-    }
-    
-    if ( [fX264optRefPopUp indexOfSelectedItem] < 3)
-    {
-        /* Only do mixed-refs when there are at least 2 refs to mix. */
-        [[fX264optMixedRefsSwitch animator] setHidden:YES];
-        [[fX264optMixedRefsLabel animator] setHidden:YES];
-        if ( [fX264optMixedRefsSwitch state] == 1 && sender != fX264optMixedRefsSwitch )
-            [fX264optMixedRefsSwitch performClick:self];
-    }
-    else
-    {
-        [[fX264optMixedRefsSwitch animator] setHidden:NO];
-        [[fX264optMixedRefsLabel animator] setHidden:NO];
-    }
-    
-    if ( [fX264optMotionEstPopUp indexOfSelectedItem] < 3 )
-    {
-        /* ME-range can only be above 16 if me >= umh
-          and changing it to < 16 is idiotic so hide it . */
-        [[fX264optMERangePopUp animator] setHidden:YES];
-        [[fX264optMERangeLabel animator] setHidden:YES];
-        if (sender != fX264optMERangePopUp && [fX264optMERangePopUp indexOfSelectedItem] > 0 )
+        else
         {
-            [fX264optMERangePopUp selectItemAtIndex:0];
-            [[fX264optMERangePopUp cell] performClick:self];
+            if( [fX264optBPyramidPopUp isHidden] == true )
+            {
+                [[fX264optBPyramidPopUp animator] setHidden:NO];
+                [[fX264optBPyramidLabel animator] setHidden:NO];
+            }
+
+            if( [fX264optDirectPredPopUp isHidden] == true )
+            {
+                [[fX264optDirectPredPopUp animator] setHidden:NO];
+                [[fX264optDirectPredLabel animator] setHidden:NO];
+            }
+            
+            if( [fX264optBAdaptPopUp isHidden] == true )
+            {
+                [[fX264optBAdaptPopUp animator] setHidden:NO];
+                [[fX264optBAdaptLabel animator] setHidden:NO];
+            }
         }
     }
-    else
-    {
-        [[fX264optMERangePopUp animator] setHidden:NO];
-        [[fX264optMERangeLabel animator] setHidden:NO];
-    }
     
-    if( [fX264optSubmePopUp indexOfSelectedItem] != 0 && [fX264optSubmePopUp indexOfSelectedItem] < 7 )
+    if( sender == fX264optMotionEstPopUp || sender == nil || sender == fDisplayX264Options )
     {
-        /* No Psy-RDO or Psy=trel if subme < 6. */
-        if( [fX264optPsyRDSlider isHidden] == false )
+        if ( [fX264optMotionEstPopUp indexOfSelectedItem] < 3 )
         {
-            [[fX264optPsyRDSlider animator] setHidden:YES];
-            [[fX264optPsyRDLabel animator] setHidden:YES];
-            [[fX264optPsyRDSlider animator] setFloatValue:1];
-            if ( (sender != fX264optPsyRDSlider) && (sender != fX264optPsyTrellisSlider) )
+            /* ME-range can only be above 16 if me >= umh
+              and changing it to < 16 is idiotic so hide it . */
+            if( [fX264optMERangePopUp isHidden] == false )
             {
-                [[fX264optPsyRDSlider cell] performClick:self];            
+                [[fX264optMERangePopUp animator] setHidden:YES];
+                [[fX264optMERangeLabel animator] setHidden:YES];
+                if ( [fX264optMERangePopUp indexOfSelectedItem] > 0 )
+                {
+                    [fX264optMERangePopUp selectItemAtIndex:0];
+                    [[fX264optMERangePopUp cell] performClick:self];
+                }
             }
         }
-        
-        if( [fX264optPsyTrellisSlider isHidden] == false)
+        else
         {
-            [[fX264optPsyTrellisSlider animator] setHidden:YES];
-            [[fX264optPsyTrellisLabel animator] setHidden:YES];
-            [[fX264optPsyTrellisSlider animator] setFloatValue:0];
-            if ( (sender != fX264optPsyTrellisSlider) && (sender != fX264optPsyRDSlider) )
+            if( [fX264optMERangePopUp isHidden] == true )
             {
-                [[fX264optPsyTrellisSlider cell] performClick:self];
+                [[fX264optMERangePopUp animator] setHidden:NO];
+                [[fX264optMERangeLabel animator] setHidden:NO];
             }
         }
     }
-    else
+    
+    if( sender == fX264optSubmePopUp || sender == nil || sender == fDisplayX264Options )
     {
-        if( [fX264optPsyRDSlider isHidden] == true )
+        if( [fX264optSubmePopUp indexOfSelectedItem] != 0 && [fX264optSubmePopUp indexOfSelectedItem] < 7 )
         {
-            [[fX264optPsyRDSlider animator] setHidden:NO];
-            [[fX264optPsyRDLabel animator] setHidden:NO];
+            /* No Psy-RDO or Psy=trel if subme < 6. */
+            if( [fX264optPsyRDSlider isHidden] == false )
+            {
+                [[fX264optPsyRDSlider animator] setHidden:YES];
+                [[fX264optPsyRDLabel animator] setHidden:YES];
+                if ( [fX264optPsyRDSlider floatValue] < 1.0 )
+                {
+                    [fX264optPsyRDSlider setFloatValue:1.0];
+                    [[fX264optPsyRDSlider cell] performClick:self];            
+                }
+            }
+
+            if( [fX264optPsyTrellisSlider isHidden] == false)
+            {
+                [[fX264optPsyTrellisSlider animator] setHidden:YES];
+                [[fX264optPsyTrellisLabel animator] setHidden:YES];
+                if ( [fX264optPsyTrellisSlider floatValue] > 0.0 )
+                {
+                    [fX264optPsyTrellisSlider setFloatValue:0.0];
+                    [[fX264optPsyTrellisSlider cell] performClick:self];
+                }
+            }
         }
-        
-        if( [fX264optTrellisPopUp indexOfSelectedItem] >= 2 && [fX264optCabacSwitch state] == true && [fX264optPsyTrellisSlider isHidden] == true )
+        else
         {
-            [[fX264optPsyTrellisSlider animator] setHidden:NO];
-            [[fX264optPsyTrellisLabel animator] setHidden:NO];
+            if( [fX264optPsyRDSlider isHidden] == true )
+            {
+                [[fX264optPsyRDSlider animator] setHidden:NO];
+                [[fX264optPsyRDLabel animator] setHidden:NO];
+            }
+
+            if( ( [fX264optTrellisPopUp indexOfSelectedItem] == 0 || [fX264optTrellisPopUp indexOfSelectedItem] >= 2 ) && [fX264optPsyTrellisSlider isHidden] == true )
+            {
+                [[fX264optPsyTrellisSlider animator] setHidden:NO];
+                [[fX264optPsyTrellisLabel animator] setHidden:NO];
+            }
         }
     }
     
-    if( [fX264optTrellisPopUp indexOfSelectedItem] < 2 )
+    if( sender == fX264optTrellisPopUp || sender == nil || sender == fDisplayX264Options )
     {
-        if( [fX264optPsyTrellisSlider isHidden] == false )
+        if( [fX264optTrellisPopUp indexOfSelectedItem] > 0 && [fX264optTrellisPopUp indexOfSelectedItem] < 2 )
         {
-            /* No Psy-trellis without trellis. */
-            [[fX264optPsyTrellisSlider animator] setHidden:YES];
-            [[fX264optPsyTrellisLabel animator] setHidden:YES];
-            [[fX264optPsyTrellisSlider animator] setFloatValue:0];
-            if ( (sender != fX264optTrellisPopUp) && (sender != fX264optPsyTrellisSlider) )
+            if( [fX264optPsyTrellisSlider isHidden] == false )
             {
-                [[fX264optPsyTrellisSlider cell] performClick:self];            
+                /* No Psy-trellis without trellis. */
+                [[fX264optPsyTrellisSlider animator] setHidden:YES];
+                [[fX264optPsyTrellisLabel animator] setHidden:YES];
+                [[fX264optPsyTrellisSlider animator] setFloatValue:0.0];
+                [[fX264optPsyTrellisSlider cell] performClick:self];
             }
         }
-    }
-    else
-    {
-        if( ( [fX264optSubmePopUp indexOfSelectedItem] == 0 || [fX264optSubmePopUp indexOfSelectedItem] >= 7 ) && [fX264optCabacSwitch state] == true  && [fX264optPsyTrellisSlider isHidden] == true )
+        else
         {
-            [[fX264optPsyTrellisSlider animator] setHidden:NO];
-            [[fX264optPsyTrellisLabel animator] setHidden:NO];
+            if( ( [fX264optSubmePopUp indexOfSelectedItem] == 0 || [fX264optSubmePopUp indexOfSelectedItem] >= 7 ) && [fX264optPsyTrellisSlider isHidden] == true )
+            {
+                [[fX264optPsyTrellisSlider animator] setHidden:NO];
+                [[fX264optPsyTrellisLabel animator] setHidden:NO];
+            }
         }
     }
 }
                 /*ref NSPopUpButton*/
                 if ([optName isEqualToString:@"ref"])
                 {
-                    [fX264optRefPopUp selectItemAtIndex:[optValue intValue]+1];
+                    // Clamp values to a minimum of 1 and a maximum of 16
+                    if ( [optValue intValue] < 1 )
+                    {
+                        [fX264optRefPopUp selectItemAtIndex:1];
+                        [ self X264AdvancedOptionsChanged: fX264optRefPopUp];
+                    }
+                    else if ( [optValue intValue] > 16 )
+                    {
+                        [fX264optRefPopUp selectItemAtIndex:16];
+                        [ self X264AdvancedOptionsChanged: fX264optRefPopUp];
+                    }
+                    else
+                    {
+                        [fX264optRefPopUp selectItemAtIndex:[optValue intValue]];
+                    }
                 }
-                /*No Fast PSkip NSButton*/
-                if ([optName isEqualToString:@"no-fast-pskip"])
+                /*WeightP NSButton*/
+                if ([optName isEqualToString:@"weightp"])
                 {
-                    [fX264optNfpskipSwitch setState:[optValue intValue]];
+                    if ([optValue intValue] < 1)
+                        [fX264optWeightPSwitch setState:0];
+                    else
+                        [fX264optWeightPSwitch setState:1];
                 }
                 /*No Dict Decimate NSButton*/
                 if ([optName isEqualToString:@"no-dct-decimate"])
                 {
                     [fX264optTrellisPopUp selectItemAtIndex:[optValue intValue]+1];
                 }
-                /*Mixed Refs NSButton*/
-                if ([optName isEqualToString:@"mixed-refs"])
-                {
-                    [fX264optMixedRefsSwitch setState:[optValue intValue]];
-                }
                 /*Motion Estimation NSPopUpButton*/
                 if ([optName isEqualToString:@"me"])
                 {
                 {
                     [fX264optMERangePopUp selectItemAtIndex:[optValue intValue]-3];
                 }
-                /*Weighted B-Frames NSButton*/
-                if ([optName isEqualToString:@"weightb"])
+                /* Adaptive B-Frames NSPopUpButton*/
+                if ([optName isEqualToString:@"b-adapt"])
                 {
-                    [fX264optWeightBSwitch setState:[optValue intValue]];
+                    [fX264optBAdaptPopUp selectItemAtIndex:[optValue intValue]+1];
                 }
                 /*B Pyramid NSPButton*/
                 if ([optName isEqualToString:@"b-pyramid"])
                 {
-                    [fX264optBPyramidSwitch setState:[optValue intValue]];
-                }
+                    
+                    if( [optValue isEqualToString:@"normal"] )
+                    {
+                        [self X264AdvancedOptionsChanged: fX264optBPyramidPopUp];
+                        [fX264optBPyramidPopUp selectItemAtIndex:0];
+                    }
+                    else if( [optValue isEqualToString:@"2"] )
+                    {
+                        [fX264optBPyramidPopUp selectItemAtIndex:0];
+                        [self X264AdvancedOptionsChanged: fX264optBPyramidPopUp];
+                    }
+                    if( [optValue isEqualToString:@"strict"] )
+                    {
+                        [fX264optBPyramidPopUp selectItemAtIndex:2];
+                    }
+                    else if( [optValue isEqualToString:@"1"] )
+                    {
+                        [fX264optBPyramidPopUp selectItemAtIndex:2];
+                        [self X264AdvancedOptionsChanged: fX264optBPyramidPopUp];
+                    }
+                    if( [optValue isEqualToString:@"none"] )
+                    {
+                        [fX264optBPyramidPopUp selectItemAtIndex:1];
+                    }
+                    else if( [optValue isEqualToString:@"0"] )
+                    {
+                        [fX264optBPyramidPopUp selectItemAtIndex:1];
+                        [self X264AdvancedOptionsChanged: fX264optBPyramidPopUp];
+                    }
+                }                
                 /*Direct B-frame Prediction NSPopUpButton*/
-                if ([optName isEqualToString:@"direct"])
+                                if ([optName isEqualToString:@"direct"])
                 {
                     if ([optValue isEqualToString:@"none"])
                         [fX264optDirectPredPopUp selectItemAtIndex:1];
                 {
                     if ([optValue isEqualToString:@"p8x8,b8x8,i8x8,i4x4"])
                     {
-                        /* Default ("some") */
+                        /* Default ("most") */
                         [fX264optAnalysePopUp selectItemAtIndex:0];
                     }
-                    if ([optValue isEqualToString:@"none"])
+                    else if ([optValue isEqualToString:@"i4x4,i8x8"] ||
+                             [optValue isEqualToString:@"i8x8,i4x4"] )
+                    {
+                        /* Some */
+                        [fX264optAnalysePopUp selectItemAtIndex:2];
+                    }
+                    else if ([optValue isEqualToString:@"none"])
                     {
                         [fX264optAnalysePopUp selectItemAtIndex:1];
                     }
-                    if ([optValue isEqualToString:@"all"])
+                    else if ([optValue isEqualToString:@"all"])
                     {
-                        [fX264optAnalysePopUp selectItemAtIndex:2];
+                        [fX264optAnalysePopUp selectItemAtIndex:3];
                     }
+                    
                 }
                 /* 8x8 DCT NSButton */
                 if ([optName isEqualToString:@"8x8dct"])
                 {
                     [fX264optCabacSwitch setState:[optValue intValue]];
                 }
+                /* Adaptive Quantization Strength NSSlider */
+                if ([optName isEqualToString:@"aq-strength"])
+                {
+                    [fX264optAqSlider setFloatValue:[optValue floatValue]];
+                }                                                              
                 /* Psy-RD and Psy-Trellis NSSliders */
                 if ([optName isEqualToString:@"psy-rd"])
                 {
     }
 }
 
-/**
- * Resets the option string to mirror the GUI widgets.
- */
-- (IBAction) X264AdvancedOptionsChanged: (id) sender
+- (NSString *) X264AdvancedOptionsOptIDToString: (id) widget
 {
     /*Determine which outlet is being used and set optName to process accordingly */
     NSString * optNameToChange = @""; // The option name such as "bframes"
     
-    if (sender == fX264optBframesPopUp)
+    if (widget == fX264optBframesPopUp)
     {
         optNameToChange = @"bframes";
     }
-    if (sender == fX264optRefPopUp)
+    if (widget == fX264optRefPopUp)
     {
         optNameToChange = @"ref";
     }
-    if (sender == fX264optNfpskipSwitch)
+    if (widget == fX264optWeightPSwitch)
     {
-        optNameToChange = @"no-fast-pskip";
+        optNameToChange = @"weightp";
     }
-    if (sender == fX264optNodctdcmtSwitch)
+    if (widget == fX264optNodctdcmtSwitch)
     {
         optNameToChange = @"no-dct-decimate";
     }
-    if (sender == fX264optSubmePopUp)
+    if (widget == fX264optSubmePopUp)
     {
         optNameToChange = @"subq";
     }
-    if (sender == fX264optTrellisPopUp)
+    if (widget == fX264optTrellisPopUp)
     {
         optNameToChange = @"trellis";
     }
-    if (sender == fX264optMixedRefsSwitch)
-    {
-        optNameToChange = @"mixed-refs";
-    }
-    if (sender == fX264optMotionEstPopUp)
+    if (widget == fX264optMotionEstPopUp)
     {
         optNameToChange = @"me";
     }
-    if (sender == fX264optMERangePopUp)
+    if (widget == fX264optMERangePopUp)
     {
         optNameToChange = @"merange";
     }
-    if (sender == fX264optWeightBSwitch)
+    if (widget == fX264optBAdaptPopUp)
     {
-        optNameToChange = @"weightb";
+        optNameToChange = @"b-adapt";
     }
-    if (sender == fX264optBPyramidSwitch)
+    if (widget == fX264optBPyramidPopUp)
     {
         optNameToChange = @"b-pyramid";
     }
-    if (sender == fX264optDirectPredPopUp)
+    if (widget == fX264optDirectPredPopUp)
     {
         optNameToChange = @"direct";
     }
-    if (sender == fX264optAlphaDeblockPopUp)
+    if (widget == fX264optAlphaDeblockPopUp)
     {
         optNameToChange = @"deblock";
     }
-    if (sender == fX264optBetaDeblockPopUp)
+    if (widget == fX264optBetaDeblockPopUp)
     {
         optNameToChange = @"deblock";
     }        
-    if (sender == fX264optAnalysePopUp)
+    if (widget == fX264optAnalysePopUp)
     {
         optNameToChange = @"analyse";
     }
-    if (sender == fX264opt8x8dctSwitch)
+    if (widget == fX264opt8x8dctSwitch)
     {
         optNameToChange = @"8x8dct";
     }
-    if (sender == fX264optCabacSwitch)
+    if (widget == fX264optCabacSwitch)
     {
         optNameToChange = @"cabac";
     }
-    if( sender == fX264optPsyRDSlider)
+    if( widget == fX264optAqSlider)
+    {
+        optNameToChange = @"aq-strength";
+    }
+    if( widget == fX264optPsyRDSlider)
     {
         optNameToChange = @"psy-rd";
     }
-    if( sender == fX264optPsyTrellisSlider)
+    if( widget == fX264optPsyTrellisSlider)
     {
         optNameToChange = @"psy-rd";
     }
     
+    return optNameToChange;
+}
+
+- (NSString *) X264AdvancedOptionsWidgetToString: (NSString *) optName withID: (id) sender
+{
+    NSString * thisOpt = @""; // The option=value string the method will return
+    
+    if ([optName isEqualToString:@"deblock"])
+    {
+        if ((([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 7)) && (([fX264optBetaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optBetaDeblockPopUp indexOfSelectedItem] == 7)))
+        {
+            /* When both deblock widgets are 0 or default or a mix of the two,
+               use a blank string, since deblocking defaults to 0,0.           */
+            thisOpt = @"";                                
+        }
+        else
+        {
+            /* Otherwise the format is deblock=a,b, where a and b both have an array
+               offset of 7 because deblocking values start at -6 instead of at zero. */
+            thisOpt = [NSString stringWithFormat:@"%@=%d,%d",optName, ([fX264optAlphaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optAlphaDeblockPopUp indexOfSelectedItem]-7 : 0,([fX264optBetaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optBetaDeblockPopUp indexOfSelectedItem]-7 : 0];
+        }
+    }
+    
+    else if ([optName isEqualToString:@"aq-strength"])
+    {
+        if( [fX264optAqSlider floatValue] == 1.0 ) 
+        {
+            /* When Aq is 1 it's the default value and can be ignored. */
+            thisOpt = @"";                                
+        }
+        else
+        {
+            thisOpt = [NSString stringWithFormat:@"%@=%0.1f", optName, [fX264optAqSlider floatValue] ];
+        }
+    }
+
+    else if ([optName isEqualToString:@"psy-rd"])
+    {
+        if( [fX264optPsyRDSlider floatValue] == 1.0 && [fX264optPsyTrellisSlider floatValue] == 0.0 ) 
+        {
+            /* When  PsyRD is 1 and PsyTrel is 0 they're default values and can be ignored. */
+            thisOpt = @"";                                
+        }
+        else
+        {
+            /* Otherwise the format is psy-rd=a,b */
+            thisOpt = [NSString stringWithFormat:@"%@=%0.1f,%0.2f", optName, [fX264optPsyRDSlider floatValue], [fX264optPsyTrellisSlider floatValue] ];
+        }
+    }
     
-    /* Set widgets depending on the opt string in field */
-    NSString * thisOpt; // The separated option such as "bframes=3"
-    NSString * optName = @""; // The option name such as "bframes"
-    NSString * optValue = @"";// The option value such as "3"
-    NSArray *currentOptsArray;
+    else if /*Boolean Switches*/ ( [optName isEqualToString:@"no-dct-decimate"] )
+    {
+        /* Here is where we take care of the boolean options that work overtly:
+           no-dct-decimate being on means no-dct-decimate=1, etc. Some options
+           require the inverse, but those will be handled a couple lines down. */
+        if ([sender state] == 0)
+        {
+            /* When these options are false, don't include them. They all default
+               to being set off, so they don't need to be mentioned at all.       */
+            thisOpt = @"";
+        }
+        else
+        {
+            /* Otherwise, include them as optioname=1 */
+            thisOpt = [NSString stringWithFormat:@"%@=%d",optName,1];
+        }
+    }
     
-    /*First, we get an opt string to process */
-    NSString *currentOptString = [fDisplayX264Options stringValue];
+    else if ( [optName isEqualToString:@"8x8dct"] || [optName isEqualToString:@"cabac"] || [optName isEqualToString:@"weightp"] )
+    {
+        /* These options default to being on. That means they
+           only need to be included in the string when turned off. */
+        if ([sender state] == 1)
+        {
+            /* It's true so don't include it. */
+            thisOpt = @"";
+        }
+        else
+        {
+            /* Otherwise, include cabac=0, etc, in the string. */
+            thisOpt = [NSString stringWithFormat:@"%@=%d",optName,0];
+        }
+    }
+                                            
+    else if (([sender indexOfSelectedItem] == 0) && (sender != fX264optAlphaDeblockPopUp) && (sender != fX264optBetaDeblockPopUp) ) // means that "unspecified" is chosen, lets then remove it from the string
+    {
+        /* When a widget is at index 0, it's default. Default means don't add to the string.
+           The exception for deblocking is because for those, *both* need to at index 0
+           for it to default, so it's handled separately, above this section.                */
+        thisOpt = @"";
+    }
     
-    /* There are going to be a few possibilities.
-       - The option might start off the string.
-       - The option might be in the middle of the string.
-       - The option might not be in the string at all yet.
-       - The string itself might not yet exist.
-       
-       Because each of these possibilities means constructing a different kind of string,
-       they're all handled separately in a sea of messy, somewhat redundant code. =(     */
-       
-    /* If the option is in the string but not the beginning of it, it will be in the form of ":optName=value"
-       so we really want to be looking for ":optNameToChange=" rather than "optNameToChange".                 */
+    else if ([optName isEqualToString:@"me"])
+    {
+        /* Motion estimation uses string values, so this switch
+           pairs the widget index with the right value string.  */
+        switch ([sender indexOfSelectedItem])
+        {   
+            case 1:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"dia"];
+                break;
+                
+            case 2:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"hex"];
+                break;
+                
+            case 3:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"umh"];
+                break;
+                
+            case 4:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"esa"];
+                break;
+            
+            case 5:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"tesa"];
+            
+            default:
+                break;
+        }
+    }
+    
+    else if ([optName isEqualToString:@"direct"])
+    {
+        /* Direct prediction uses string values, so this switch
+           pairs the right string value with the right widget index. */
+        switch ([sender indexOfSelectedItem])
+        {   
+            case 1:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
+                break;
+                
+            case 2:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"spatial"];
+                break;
+                
+            case 3:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"temporal"];
+                break;
+                
+            case 4:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"auto"];
+                break;
+                
+            default:
+                break;
+        }
+    }
+    
+    else if ([optName isEqualToString:@"analyse"])
+    {
+        /* Analysis uses string values as well. */
+        switch ([sender indexOfSelectedItem])
+        {   
+            case 1:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
+                break;
+            case 2:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"i4x4,i8x8"];
+                break;
+            case 3:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"all"];
+                break;
+                
+            default:
+                break;
+        }
+    }
+
+    else if ([optName isEqualToString:@"b-pyramid"])
+    {
+        /* B-pyramid uses string values too. */
+        switch ([sender indexOfSelectedItem])
+        {   
+            case 1:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
+                break;
+            case 2:
+                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"strict"];
+                break;
+            case 0:
+                thisOpt = @"";
+                break;
+                
+            default:
+                break;
+        }
+    }
+    
+    else if ([optName isEqualToString:@"merange"])
+    {
+        /* Motion estimation range uses an odd array offset because in addition
+           to starting with index 0 as default, index 1 starts at 4 instead of 1,
+           because merange can't go below 4. So it has to be handled separately.  */
+        thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]+3];
+    }
+    
+    else if ([optName isEqualToString:@"b-adapt"])
+    {
+        /* B-adapt starts at index 0 with default then goes 0, 1, 2)*/
+        thisOpt = [NSString stringWithFormat:@"%@=%d", optName, [sender indexOfSelectedItem]-1];
+    }
+    
+    else if ([optName isEqualToString:@"ref"])
+    {
+        /* Refs use actual index numbers */
+        thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]];
+    }
+    
+    else // we have a valid value to change, so change it
+    {
+        if ( [sender indexOfSelectedItem] != 0 )
+        /* Here's our general case, that catches things like b-frames.
+           Basically, any options that are PopUp menus with index 0 as default and
+           index 1 as 0, with numerical values, are all handled right here. All of
+           the above stuff is for the exceptions to the general case.              */
+            thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]-1];
+    }
+    
+    return thisOpt;
+}
+
+- (BOOL) X264AdvancedOptionsIsOpt: (NSString *) optNameToChange inString: (NSString *) currentOptString
+{
+    /* If the option is in the string but not the beginning of it,
+       it will be in the form of ":optName=value" so we really want
+       to be looking for ":optNameToChange=" rather than "optNameToChange". */
     NSString *checkOptNameToChange = [NSString stringWithFormat:@":%@=",optNameToChange];
     
     /* Now we store the part of the string up through the option name in currentOptRange. */
         If the range is 0, it's the first option listed in the string.       */        
     NSString *checkOptNameToChangeBeginning = [NSString stringWithFormat:@"%@=",optNameToChange];
     NSRange currentOptRangeBeginning = [currentOptString rangeOfString:checkOptNameToChangeBeginning];
-    
+
     if (currentOptRange.location != NSNotFound || currentOptRangeBeginning.location == 0)
+        return true;
+    else
+        return false;
+} 
+
+/**
+ * Resets the option string to mirror the GUI widgets.
+ */
+- (IBAction) X264AdvancedOptionsChanged: (id) sender
+{
+    /* Look up the equivalent string option name of the calling widget. */
+    NSString * optNameToChange = [self X264AdvancedOptionsOptIDToString: sender];
+    
+    NSString * thisOpt = @"";  // The separated option such as "bframes=3"
+    NSString * optName = @"";  // The option name such as "bframes"
+    NSString * optValue = @""; // The option value such as "3"
+    NSArray *currentOptsArray;
+    
+    /* Get the current opt string being displayed. */
+    NSString *currentOptString = [fDisplayX264Options stringValue];
+    
+    /* There are going to be a few possibilities.
+       - The option might start off the string.
+       - The option might be in the middle of the string.
+       - The option might not be in the string at all yet.
+       - The string itself might not yet exist.             */
+    
+    if( [self X264AdvancedOptionsIsOpt: optNameToChange inString: currentOptString] )
     {
         /* If the option is in the string wth a semicolon, or starts the string, it's time to edit.
            This means parsing the whole string into an array of options and values. From there,
         /* Create new empty opt string*/
         NSString *changedOptString = @"";
         
-        /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/
+        /* Put individual options into an array based on the ":"
+           separator for processing, result is "<opt>=<value>"   */
         currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
         
-        /*iterate through the array and get <opts> and <values*/
+        /* Iterate through the array and get <opts> and <values*/
         int loopcounter;
         int currentOptsArrayCount = [currentOptsArray count];
         for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
                    
                 optName = [thisOpt substringToIndex:splitOptRange.location];
                 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
-                
-                /*Run through the available widgets for x264 opts and set them, as you add widgets, 
-                    they need to be added here. This should be moved to its own method probably*/
-                
+
                 /*If the optNameToChange is found, appropriately change the value or delete it if
                     "Unspecified" is set.*/
                 if ([optName isEqualToString:optNameToChange])
                 {
-                    if ([optNameToChange isEqualToString:@"deblock"])
-                    {
-                        if ((([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 7)) && (([fX264optBetaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optBetaDeblockPopUp indexOfSelectedItem] == 7)))
-                        {
-                            /* When both deblock widgets are 0 or default or a mix of the two,
-                               use a blank string, since deblocking defaults to 0,0.           */
-                            thisOpt = @"";                                
-                        }
-                        else
-                        {
-                            /* Otherwise the format is deblock=a,b, where a and b both have an array
-                               offset of 7 because deblocking values start at -6 instead of at zero. */
-                            thisOpt = [NSString stringWithFormat:@"%@=%d,%d",optName, ([fX264optAlphaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optAlphaDeblockPopUp indexOfSelectedItem]-7 : 0,([fX264optBetaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optBetaDeblockPopUp indexOfSelectedItem]-7 : 0];
-                        }
-                    }
-                    if ([optNameToChange isEqualToString:@"psy-rd"])
-                    {
-                        if( [fX264optPsyRDSlider floatValue] == 1.0 && [fX264optPsyTrellisSlider floatValue] == 0.0 ) 
-                        {
-                            /* When  PsyRD is 1 and PsyTrel is 0 they're default values and can be ignored. */
-                            thisOpt = @"";                                
-                        }
-                        else
-                        {
-                            /* Otherwise the format is deblock=a,b, where a and b both have an array
-                               offset of 7 because deblocking values start at -6 instead of at zero. */
-                            thisOpt = [NSString stringWithFormat:@"%@=%0.1f,%0.1f", optName, [fX264optPsyRDSlider floatValue], [fX264optPsyTrellisSlider floatValue] ];
-                        }
-                    }
-                    else if /*Boolean Switches*/ ([optNameToChange isEqualToString:@"mixed-refs"] || [optNameToChange isEqualToString:@"weightb"] ||  [optNameToChange isEqualToString:@"b-pyramid"] || [optNameToChange isEqualToString:@"no-fast-pskip"] || [optNameToChange isEqualToString:@"no-dct-decimate"] || [optNameToChange isEqualToString:@"8x8dct"] )
-                    {
-                        /* Here is where we take care of the boolean options that work overtly:
-                           no-dct-decimate being on means no-dct-decimate=1, etc. Some options
-                           require the inverse, but those will be handled a couple lines down. */
-                        if ([sender state] == 0)
-                        {
-                            /* When these options are false, don't include them. They all default
-                               to being set off, so they don't need to be mentioned at all.       */
-                            thisOpt = @"";
-                        }
-                        else
-                        {
-                            /* Otherwise, include them as optioname=1 */
-                            thisOpt = [NSString stringWithFormat:@"%@=%d",optName,1];
-                        }
-                    }
-                    else if ([optNameToChange isEqualToString:@"cabac"])
-                    {
-                        /* CABAC is odd, in that it defaults to being on. That means
-                           it only needs to be included in the string when turned off. */
-                        if ([sender state] == 1)
-                        {
-                            /* It's true so don't include it. */
-                            thisOpt = @"";
-                        }
-                        else
-                        {
-                            /* Otherwise, include cabac=0 in the string to enable CAVLC. */
-                            thisOpt = [NSString stringWithFormat:@"%@=%d",optName,0];
-                        }
-                    }                                        
-                    else if (([sender indexOfSelectedItem] == 0) && (sender != fX264optAlphaDeblockPopUp) && (sender != fX264optBetaDeblockPopUp) ) // means that "unspecified" is chosen, lets then remove it from the string
-                    {
-                        /* When a widget is at index 0, it's default. Default means don't add to the string.
-                           The exception for deblocking is because for those, *both* need to at index 0
-                           for it to default, so it's handled separately, above this section.                */
-                        thisOpt = @"";
-                    }
-                    else if ([optNameToChange isEqualToString:@"me"])
-                    {
-                        /* Motion estimation uses string values, so this switch
-                           pairs the widget index with the right value string.  */
-                        switch ([sender indexOfSelectedItem])
-                        {   
-                            case 1:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"dia"];
-                                break;
-                                
-                            case 2:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"hex"];
-                                break;
-                                
-                            case 3:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"umh"];
-                                break;
-                                
-                            case 4:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"esa"];
-                                break;
-                            
-                            case 5:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"tesa"];
-                            
-                            default:
-                                break;
-                        }
-                    }
-                    else if ([optNameToChange isEqualToString:@"direct"])
-                    {
-                        /* Direct prediction uses string values, so this switch
-                           pairs the right string value with the right widget index. */
-                        switch ([sender indexOfSelectedItem])
-                        {   
-                            case 1:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
-                                break;
-                                
-                            case 2:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"spatial"];
-                                break;
-                                
-                            case 3:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"temporal"];
-                                break;
-                                
-                            case 4:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"auto"];
-                                break;
-                                
-                            default:
-                                break;
-                        }
-                    }
-                    else if ([optNameToChange isEqualToString:@"analyse"])
-                    {
-                        /* Analysis uses string values as well. */
-                        switch ([sender indexOfSelectedItem])
-                        {   
-                            case 1:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
-                                break;
-                                
-                            case 2:
-                                thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"all"];
-                                break;
-                                
-                            default:
-                                break;
-                        }
-                    }
-                    else if ([optNameToChange isEqualToString:@"merange"])
-                    {
-                        /* Motion estimation range uses an odd array offset because in addition
-                           to starting with index 0 as default, index 1 starts at 4 instead of 1,
-                           because merange can't go below 4. So it has to be handled separately.  */
-                        thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]+3];
-                    }
-                    else // we have a valid value to change, so change it
-                    {
-                        if ( [sender indexOfSelectedItem] != 0 )
-                        /* Here's our general case, that catches things like ref frames and b-frames.
-                           Basically, any options that are PopUp menus with index 0 as default and
-                           index 1 as 1, with numerical values, are all handled right here. All of
-                           the above stuff is for the exceptions to the general case.              */
-                            thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]-1];
-                    }
+                    thisOpt = [self X264AdvancedOptionsWidgetToString: optName withID: sender];
                 }
             }
             
         }
         
         /* Change the dislayed option string to reflect the new modified settings */
-        [fDisplayX264Options setStringValue:[NSString stringWithFormat:changedOptString]];    
+        [fDisplayX264Options setStringValue:changedOptString];    
     }
     else // if none exists, add it to the string
     {
         /* This is where options that aren't already in the string are handled. */
         if ([[fDisplayX264Options stringValue] isEqualToString: @""])
         {
-            /* The option might not be in the string because the
-               string is empty. Handle this possibility first.   */
-            if ([optNameToChange isEqualToString:@"me"])
-            {
-                /* Special case for motion estimation, which uses string values
-                   that need to be paired up with the equivalent widget index.  */
-                switch ([sender indexOfSelectedItem])
-                {   
-                    case 1:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"dia"]]];
-                        break;
-                        
-                    case 2:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"hex"]]];
-                        break;
-                        
-                    case 3:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"umh"]]];
-                        break;
-                        
-                    case 4:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"esa"]]];
-                        break;
-                    
-                    case 5:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"tesa"]]];
-                        break;
-                    
-                    default:
-                        break;
-                }
-            }
-            else if ([optNameToChange isEqualToString:@"direct"])
-            {
-                /* Special case for direct prediction, which uses string values
-                   that need to be paired up with the equivalent widget index.  */
-                switch ([sender indexOfSelectedItem])
-                {   
-                    case 1:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
-                        break;
-                        
-                    case 2:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"spatial"]]];
-                        break;
-                        
-                    case 3:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"temporal"]]];
-                        break;
-                        
-                    case 4:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"auto"]]];
-                        break;
-                        
-                    default:
-                        break;
-                }
-            }
-            else if ([optNameToChange isEqualToString:@"analyse"])
-            {
-                /* Special case for partition analysis, which uses string values
-                   that need to be paired up with the equivalent widget index.  */
-                switch ([sender indexOfSelectedItem])
-                {   
-                    case 1:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
-                        break;
-                        
-                    case 2:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"all"]]];
-                        break;
-                        
-                    default:
-                        break;
-                }
-            }
             
-            else if ([optNameToChange isEqualToString:@"merange"])
-            {
-                /* Special case for motion estimation range, which uses
-                   a widget index offset of 3. This is because the
-                   first valid value after default is four, not zero.   */
-                [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                    [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]+3]]];
-            }
-            else if ([optNameToChange isEqualToString:@"deblock"])
-            {
-                /* Very special case for deblock. Uses a weird widget index offset
-                   of 7, because the first value after default is -6, rather than 0.
-                   As well, deblock only goes to default when *both* alpha and beta
-                   are zero. If only one is zero, you can't mark it down as default.
-                   Instead, mark that one down as literally 0. This is because when
-                   widgets are at default values, they aren't included in the string.
-                   If only one filter is at 0, both need to be overtly specified.    */
-                [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]]];                
-            }
-            else if ([optNameToChange isEqualToString:@"psy-rd"])
-            {
-                /* Special case for psy-rd and psy-trellis. */
-                if( [fX264optPsyRDSlider floatValue] == 1 && [fX264optPsyTrellisSlider floatValue] == 0 )
-                {
-                    /* Defaults, use null string. */
-                    [fDisplayX264Options setStringValue:@""];
-                }
-                else
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%0.1f,%0.1f", [NSString stringWithFormat:optNameToChange], [fX264optPsyRDSlider floatValue],  [fX264optPsyTrellisSlider floatValue]]];
-                }
-            }
-            else if /*Boolean Switches*/ ([optNameToChange isEqualToString:@"mixed-refs"] || [optNameToChange isEqualToString:@"weightb"] || [optNameToChange isEqualToString:@"b-pyramid"] || [optNameToChange isEqualToString:@"no-fast-pskip"] || [optNameToChange isEqualToString:@"no-dct-decimate"] || [optNameToChange isEqualToString:@"8x8dct"] )
-            {
-                /* This covers all the boolean options that need to be specified only when true. */
-                if ([sender state] == 0)
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@""]];                    
-                }
-                else
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                        [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];
-                }
-            }
-            else if ([optNameToChange isEqualToString:@"cabac"])
-            {
-                /* CABAC is weird in that you need the inverse. Only include in the string
-                   when cabac=0, because cabac=1 is the default. Turning it off means CAVLC. */
-                if ([sender state] == 1)
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@""]];                                        
-                }
-                else
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                        [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];                    
-                }
-            }            
-            else
-            {
-                if ( [sender indexOfSelectedItem] != 0 )
-                /* General case to cover all the normal PopUp widgets, like ref and b-frames. */
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@", 
-                    [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]-1]]];
-            }
+            [fDisplayX264Options setStringValue:
+                [self X264AdvancedOptionsWidgetToString: optNameToChange withID: sender]];
         }
         else
         {
-            /* The string isn't empty, and the option isn't already in it,
-               so it will need to be appended to the string with a colon.  */
-            if ([optNameToChange isEqualToString:@"me"])
-            {
-                /* Special case for motion estimation, which uses string values
-                   that need to be paired up with the equivalent widget index.  */
-                switch ([sender indexOfSelectedItem])
-                {   
-                    case 1:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"dia"]]];
-                        break;
-                        
-                    case 2:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"hex"]]];
-                        break;
-                        
-                    case 3:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"umh"]]];
-                        break;
-                        
-                    case 4:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"esa"]]];
-                        break;
-
-                    case 5:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"tesa"]]];
-                        break;
-                        
-                    default:
-                        break;
-                }
-            }
-            else if ([optNameToChange isEqualToString:@"direct"])
-            {
-                /* Special case for direct prediction, which uses string values
-                   that need to be paired up with the equivalent widget index.  */
-                switch ([sender indexOfSelectedItem])
-                {   
-                    case 1:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
-                        break;
-                        
-                    case 2:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"spatial"]]];
-                        break;
-                        
-                    case 3:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"temporal"]]];
-                        break;
-                        
-                    case 4:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"auto"]]];
-                        break;
-                        
-                    default:
-                        break;
-                }
-            }
-            else if ([optNameToChange isEqualToString:@"analyse"])
-            {
-                /* Special case for partition analysis, which uses string values
-                   that need to be paired up with the equivalent widget index.  */
-                switch ([sender indexOfSelectedItem])
-                {   
-                    case 1:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
-                        break;
-                        
-                    case 2:
-                        [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@", 
-                            [NSString stringWithFormat:[fDisplayX264Options stringValue]],
-                            [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"all"]]];
-                        break;
-                        
-                    default:
-                        break;
-                }
-            }
-            
-            else if ([optNameToChange isEqualToString:@"merange"])
-            {
-                /* Motion estimation range uses a weird offset since its index goes
-                   0: default, 1: 4, because the first valid value is 4, not 1.     */
-                [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]], 
-                    [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]+3]]];
-            }
-            else if ([optNameToChange isEqualToString:@"deblock"])
-            {
-                /* Deblock is really weird because it has two values, and if only one is default, both
-                   still need to be specified directly. with the default one at zero. To make deblock
-                   just a little more fun, values start at -6 instead of at zero.                       */
-                [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]]];                
-            }
-            else if ([optNameToChange isEqualToString:@"psy-rd"])
-            {
-                /* Special case for psy-rd and psy-trel */
-                if( [fX264optPsyRDSlider floatValue] == 1 && [fX264optPsyTrellisSlider floatValue] == 0 )
-                {
-                    /* Defaults, don't change string. */
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:[fDisplayX264Options stringValue]]];
-                }
-                else
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%0.1f,%0.1f", [NSString stringWithFormat:[fDisplayX264Options stringValue]], [NSString stringWithFormat:optNameToChange], [fX264optPsyRDSlider floatValue],  [fX264optPsyTrellisSlider floatValue]]];
-                }
-                
-            }
-            else if /*Boolean Switches*/ ([optNameToChange isEqualToString:@"mixed-refs"] || [optNameToChange isEqualToString:@"weightb"] || [optNameToChange isEqualToString:@"b-pyramid"] || [optNameToChange isEqualToString:@"no-fast-pskip"] || [optNameToChange isEqualToString:@"no-dct-decimate"] || [optNameToChange isEqualToString:@"8x8dct"] )
-            {
-                /* Covers all the normal booleans, that only need to be included in the string when they're true. */
-                if ([sender state] == 0)
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]]]];                    
-                }
-                else
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]], 
-                        [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];                
-                }
-            }
-            else if ([optNameToChange isEqualToString:@"cabac"])
-            {
-                /* CABAC is weird, in that it's an inverse. Only include it in the string when it's false. */
-                if ([sender state] == 1)
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]]]];                    
-                }
-                else
-                {
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]], 
-                        [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];
-                }
-            }
-            else
+            /* The string isn't empty, and the option isn't already in it, so
+               it will need to be appended to the current string with a colon,
+               as long as the string to be appended isn't just blank (default). */
+            if( [[self X264AdvancedOptionsWidgetToString: optNameToChange withID: sender] isEqualToString: @""] == false )
             {
-                /* General case to handle the normal PopUp widgets like ref and b-frames. */
-                if ( [sender indexOfSelectedItem] != 0 )
-                    [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]], 
-                    [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]-1]]];
+                [fDisplayX264Options setStringValue:
+                    [NSString stringWithFormat:@"%@:%@",
+                        currentOptString,
+                        [self X264AdvancedOptionsWidgetToString: optNameToChange withID: sender] ]];                
             }
         }
     }