1 /* HBAdvancedController
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. */
7 #import "HBAdvancedController.h"
9 @implementation HBAdvancedController
19 - (void) setView: (NSBox *) box
22 [fOptionsBox setContentView:fX264optView];
25 - (BOOL) loadMyNibFile
27 if(![NSBundle loadNibNamed:@"AdvancedView" owner:self])
29 NSLog(@"Warning! Could not load myNib file.\n");
36 - (NSString *) optionsString
38 return [fDisplayX264Options stringValue];
41 - (void) setOptions: (NSString *)string
43 [fDisplayX264Options setStringValue:string];
44 [self X264AdvancedOptionsSet:nil];
47 - (void) setHidden: (BOOL) hide
51 [fOptionsBox setContentView:fEmptyView];
52 [fX264optViewTitleLabel setStringValue: @"Only Used With The x264 (H.264) Codec"];
56 [fOptionsBox setContentView:fX264optView];
57 [fX264optViewTitleLabel setStringValue: @""];
62 - (void) enableUI: (bool) b
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};
76 for( i = 0; i < sizeof( controls ) / sizeof( NSControl * ); i++ )
78 if( [[controls[i] className] isEqualToString: @"NSTextField"] )
80 NSTextField * tf = (NSTextField *) controls[i];
83 [tf setTextColor: b ? [NSColor controlTextColor] :
84 [NSColor disabledControlTextColor]];
88 [controls[i] setEnabled: b];
92 [fX264optView setWantsLayer:YES];
101 * Populates the option widgets
103 - (IBAction) X264AdvancedOptionsSet: (id) sender
105 /*Set opt widget values here*/
107 /*B-Frames fX264optBframesPopUp*/
109 [fX264optBframesPopUp removeAllItems];
110 [fX264optBframesPopUp addItemWithTitle:@"Default (0)"];
113 [fX264optBframesPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
116 /*Reference Frames fX264optRefPopUp*/
117 [fX264optRefPopUp removeAllItems];
118 [fX264optRefPopUp addItemWithTitle:@"Default (1)"];
121 [fX264optRefPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
124 /*No Fast P-Skip fX264optNfpskipSwitch BOOLEAN*/
125 [fX264optNfpskipSwitch setState:0];
127 /*No Dict Decimate fX264optNodctdcmtSwitch BOOLEAN*/
128 [fX264optNodctdcmtSwitch setState:0];
130 /*Sub Me fX264optSubmePopUp*/
131 [fX264optSubmePopUp removeAllItems];
132 [fX264optSubmePopUp addItemWithTitle:@"Default (6)"];
135 [fX264optSubmePopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
138 /*Trellis fX264optTrellisPopUp*/
139 [fX264optTrellisPopUp removeAllItems];
140 [fX264optTrellisPopUp addItemWithTitle:@"Default (0)"];
143 [fX264optTrellisPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
145 [fX264optTrellisPopUp setWantsLayer:YES];
147 /*Mixed-references fX264optMixedRefsSwitch BOOLEAN*/
148 [fX264optMixedRefsSwitch setState:0];
149 [fX264optMixedRefsSwitch setWantsLayer:YES];
151 /*Motion Estimation fX264optMotionEstPopUp*/
152 [fX264optMotionEstPopUp removeAllItems];
153 [fX264optMotionEstPopUp addItemWithTitle:@"Default (Hexagon)"];
154 [fX264optMotionEstPopUp addItemWithTitle:@"Diamond"];
155 [fX264optMotionEstPopUp addItemWithTitle:@"Hexagon"];
156 [fX264optMotionEstPopUp addItemWithTitle:@"Uneven Multi-Hexagon"];
157 [fX264optMotionEstPopUp addItemWithTitle:@"Exhaustive"];
159 /*Motion Estimation range fX264optMERangePopUp*/
160 [fX264optMERangePopUp removeAllItems];
161 [fX264optMERangePopUp addItemWithTitle:@"Default (16)"];
164 [fX264optMERangePopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
167 /*Weighted B-Frame Prediction fX264optWeightBSwitch BOOLEAN*/
168 [fX264optWeightBSwitch setState:0];
169 [fX264optWeightBSwitch setWantsLayer:YES];
171 /*B-frame Pyramids fX264optBPyramidSwitch BOOLEAN*/
172 [fX264optBPyramidSwitch setState:0];
173 [fX264optBPyramidSwitch setWantsLayer:YES];
175 /*Direct B-Frame Prediction Mode fX264optDirectPredPopUp*/
176 [fX264optDirectPredPopUp removeAllItems];
177 [fX264optDirectPredPopUp addItemWithTitle:@"Default (Spatial)"];
178 [fX264optDirectPredPopUp addItemWithTitle:@"None"];
179 [fX264optDirectPredPopUp addItemWithTitle:@"Spatial"];
180 [fX264optDirectPredPopUp addItemWithTitle:@"Temporal"];
181 [fX264optDirectPredPopUp addItemWithTitle:@"Automatic"];
182 [fX264optDirectPredPopUp setWantsLayer:YES];
185 [fX264optAlphaDeblockPopUp removeAllItems];
186 [fX264optAlphaDeblockPopUp addItemWithTitle:@"Default (0)"];
189 [fX264optAlphaDeblockPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
193 [fX264optBetaDeblockPopUp removeAllItems];
194 [fX264optBetaDeblockPopUp addItemWithTitle:@"Default (0)"];
197 [fX264optBetaDeblockPopUp addItemWithTitle:[NSString stringWithFormat:@"%d",i]];
200 /* Analysis fX264optAnalysePopUp */
201 [fX264optAnalysePopUp removeAllItems];
202 [fX264optAnalysePopUp addItemWithTitle:@"Default (some)"]; /* 0=default */
203 [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"None"]]; /* 1=none */
204 [fX264optAnalysePopUp addItemWithTitle:[NSString stringWithFormat:@"All"]]; /* 2=all */
206 /* 8x8 DCT fX264op8x8dctSwitch */
207 [fX264opt8x8dctSwitch setState:0];
208 [fX264opt8x8dctSwitch setWantsLayer:YES];
210 /* CABAC fX264opCabacSwitch */
211 [fX264optCabacSwitch setState:1];
213 /* Standardize the option string */
214 [self X264AdvancedOptionsStandardizeOptString:nil];
216 /* Set Current GUI Settings based on newly standardized string */
217 [self X264AdvancedOptionsSetCurrentSettings:nil];
219 /* Fade out options that don't apply */
220 [self X264AdvancedOptionsAnimate: sender];
224 * Cleans the option string to use a standard format of option=value
226 - (IBAction) X264AdvancedOptionsStandardizeOptString: (id) sender
228 /* Set widgets depending on the opt string in field */
229 NSString * thisOpt; // The separated option such as "bframes=3"
230 NSString * optName = @""; // The option name such as "bframes"
231 NSString * optValue = @"";// The option value such as "3"
232 NSString * changedOptString = @"";
233 NSArray *currentOptsArray;
235 /*First, we get an opt string to process */
236 NSString *currentOptString = [fDisplayX264Options stringValue];
238 /* Verify there is an opt string to process by making sure an
239 option is getting its value set. If so, start to process it. */
240 NSRange currentOptRange = [currentOptString rangeOfString:@"="];
241 if (currentOptRange.location != NSNotFound)
243 /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/
244 currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
246 /*iterate through the array and get <opts> and <values*/
248 int currentOptsArrayCount = [currentOptsArray count];
249 for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
251 thisOpt = [currentOptsArray objectAtIndex:loopcounter];
253 NSRange splitOptRange = [thisOpt rangeOfString:@"="];
254 if (splitOptRange.location != NSNotFound)
256 optName = [thisOpt substringToIndex:splitOptRange.location];
257 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
259 /* Standardize the names here depending on whats in the string */
260 optName = [self X264AdvancedOptionsStandardizeOptNames:optName];
261 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,optValue];
263 else // No value given so we use a default of "1"
267 /* Standardize the names here depending on whats in the string */
268 optName = [self X264AdvancedOptionsStandardizeOptNames:optName];
269 thisOpt = [NSString stringWithFormat:@"%@=%d",optName,1];
272 /* Construct New String for opts here.*/
273 if ([thisOpt isEqualToString:@""])
275 /* Blank option, just add it to the string. (Why?) */
276 changedOptString = [NSString stringWithFormat:@"%@%@",changedOptString,thisOpt];
280 if ([changedOptString isEqualToString:@""])
282 /* Blank string, output the current option. */
283 changedOptString = [NSString stringWithFormat:@"%@",thisOpt];
287 /* Option exists and string exists, so append the option
288 to the string with a semi-colon inbetween them. */
289 changedOptString = [NSString stringWithFormat:@"%@:%@",changedOptString,thisOpt];
295 /* Change the option string to reflect the new standardized option string */
296 [fDisplayX264Options setStringValue:[NSString stringWithFormat:changedOptString]];
300 * Cleans the option string to use a standard set of option names, by conflating synonyms.
302 - (NSString *) X264AdvancedOptionsStandardizeOptNames:(NSString *) cleanOptNameString
304 /* Reference Frames */
305 if ([cleanOptNameString isEqualToString:@"ref"] || [cleanOptNameString isEqualToString:@"frameref"])
307 cleanOptNameString = @"ref";
310 /*No Fast PSkip nofast_pskip*/
311 if ([cleanOptNameString isEqualToString:@"no-fast-pskip"] || [cleanOptNameString isEqualToString:@"no_fast_pskip"] || [cleanOptNameString isEqualToString:@"nofast_pskip"])
313 cleanOptNameString = @"no-fast-pskip";
317 if ([cleanOptNameString isEqualToString:@"no-dct-decimate"] || [cleanOptNameString isEqualToString:@"no_dct_decimate"] || [cleanOptNameString isEqualToString:@"nodct_decimate"])
319 cleanOptNameString = @"no-dct-decimate";
323 if ([cleanOptNameString isEqualToString:@"subme"])
325 cleanOptNameString = @"subq";
329 if ([cleanOptNameString isEqualToString:@"me-range"] || [cleanOptNameString isEqualToString:@"me_range"])
330 cleanOptNameString = @"merange";
333 if ([cleanOptNameString isEqualToString:@"weight-b"] || [cleanOptNameString isEqualToString:@"weight_b"])
335 cleanOptNameString = @"weightb";
339 if ([cleanOptNameString isEqualToString:@"b_pyramid"])
341 cleanOptNameString = @"b-pyramid";
344 /*Direct Prediction*/
345 if ([cleanOptNameString isEqualToString:@"direct-pred"] || [cleanOptNameString isEqualToString:@"direct_pred"])
347 cleanOptNameString = @"direct";
351 if ([cleanOptNameString isEqualToString:@"filter"])
353 cleanOptNameString = @"deblock";
357 if ([cleanOptNameString isEqualToString:@"partitions"])
359 cleanOptNameString = @"analyse";
362 return cleanOptNameString;
366 * Fades options in and out depending on whether they're available..
368 - (IBAction) X264AdvancedOptionsAnimate: (id) sender
370 /* Lots of situations to cover.
371 - B-frames (when 0 turn of b-frame specific stuff, when < 2 disable b-pyramid)
372 - CABAC (when 0 turn off trellis)
373 - analysis (if none, turn off 8x8dct)
374 - refs (under 2, disable mixed-refs)
377 if ( [fX264optBframesPopUp indexOfSelectedItem ] < 2)
379 /* If the b-frame widget is at 0 or 1, the user has chosen
380 not to use b-frames at all. So disable the options
381 that can only be used when b-frames are enabled. */
382 [[fX264optWeightBSwitch animator] setHidden:YES];
383 [[fX264optWeightBLabel animator] setHidden:YES];
384 if ( [fX264optWeightBSwitch state] == 1 && sender != fX264optWeightBSwitch && sender != fX264optBPyramidSwitch && sender != fX264optDirectPredPopUp)
385 [fX264optWeightBSwitch performClick:self];
387 [[fX264optBPyramidSwitch animator] setHidden:YES];
388 [[fX264optBPyramidLabel animator] setHidden:YES];
389 if ( [fX264optBPyramidSwitch state] == 1 && sender != fX264optWeightBSwitch && sender != fX264optBPyramidSwitch && sender != fX264optDirectPredPopUp)
390 [fX264optBPyramidSwitch performClick:self];
392 [[fX264optDirectPredPopUp animator] setHidden:YES];
393 [[fX264optDirectPredLabel animator] setHidden:YES];
394 if ( [fX264optDirectPredPopUp indexOfSelectedItem] > 0 && sender != fX264optWeightBSwitch && sender != fX264optBPyramidSwitch && sender != fX264optDirectPredPopUp)
396 [fX264optDirectPredPopUp selectItemAtIndex: 0];
397 [[fX264optDirectPredPopUp cell] performClick:self];
401 else if ( [fX264optBframesPopUp indexOfSelectedItem ] == 2)
403 /* Only 1 b-frame? Disable b-pyramid. */
404 [[fX264optBPyramidSwitch animator] setHidden:YES];
405 [[fX264optBPyramidLabel animator] setHidden:YES];
406 if ( [fX264optBPyramidSwitch state] == 1 && sender != fX264optBPyramidSwitch)
407 [fX264optBPyramidSwitch performClick:self];
409 [[fX264optWeightBSwitch animator] setHidden:NO];
410 [[fX264optWeightBLabel animator] setHidden:NO];
412 [[fX264optDirectPredPopUp animator] setHidden:NO];
413 [[fX264optDirectPredLabel animator] setHidden:NO];
417 [[fX264optWeightBSwitch animator] setHidden:NO];
418 [[fX264optWeightBLabel animator] setHidden:NO];
420 [[fX264optBPyramidSwitch animator] setHidden:NO];
421 [[fX264optBPyramidLabel animator] setHidden:NO];
423 [[fX264optDirectPredPopUp animator] setHidden:NO];
424 [[fX264optDirectPredLabel animator] setHidden:NO];
427 if ( [fX264optCabacSwitch state] == false)
429 /* Without CABAC entropy coding, trellis doesn't run. */
431 [[fX264optTrellisPopUp animator] setHidden:YES];
432 [[fX264optTrellisLabel animator] setHidden:YES];
433 [fX264optTrellisPopUp selectItemAtIndex:0];
434 if (sender != fX264optTrellisPopUp)
435 [[fX264optTrellisPopUp cell] performClick:self];
439 [[fX264optTrellisPopUp animator] setHidden:NO];
440 [[fX264optTrellisLabel animator] setHidden:NO];
443 if ( [fX264optAnalysePopUp indexOfSelectedItem] == 1)
445 /* No analysis? Disable 8x8dct */
446 [[fX264opt8x8dctSwitch animator] setHidden:YES];
447 [[fX264opt8x8dctLabel animator] setHidden:YES];
448 if ( [fX264opt8x8dctSwitch state] == 1 && sender != fX264opt8x8dctSwitch )
449 [fX264opt8x8dctSwitch performClick:self];
453 [[fX264opt8x8dctSwitch animator] setHidden:NO];
454 [[fX264opt8x8dctLabel animator] setHidden:NO];
457 if ( [fX264optRefPopUp indexOfSelectedItem] < 3)
459 /* Only do mixed-refs when there are at least 2 refs to mix. */
460 [[fX264optMixedRefsSwitch animator] setHidden:YES];
461 [[fX264optMixedRefsLabel animator] setHidden:YES];
462 if ( [fX264optMixedRefsSwitch state] == 1 && sender != fX264optMixedRefsSwitch )
463 [fX264optMixedRefsSwitch performClick:self];
467 [[fX264optMixedRefsSwitch animator] setHidden:NO];
468 [[fX264optMixedRefsLabel animator] setHidden:NO];
473 * Resets the GUI widgets to the contents of the option string.
475 - (IBAction) X264AdvancedOptionsSetCurrentSettings: (id) sender
477 /* Set widgets depending on the opt string in field */
478 NSString * thisOpt; // The separated option such as "bframes=3"
479 NSString * optName = @""; // The option name such as "bframes"
480 NSString * optValue = @"";// The option value such as "3"
481 NSArray *currentOptsArray;
483 /*First, we get an opt string to process */
484 NSString *currentOptString = [fDisplayX264Options stringValue];
486 /* Verify there is an opt string to process by making sure an
487 option is getting its value set. If so, start to process it. */
488 NSRange currentOptRange = [currentOptString rangeOfString:@"="];
489 if (currentOptRange.location != NSNotFound)
491 /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/
492 currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
494 /*iterate through the array and get <opts> and <values*/
496 int currentOptsArrayCount = [currentOptsArray count];
497 for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
499 thisOpt = [currentOptsArray objectAtIndex:loopcounter];
501 /* Verify the option sets a value */
502 NSRange splitOptRange = [thisOpt rangeOfString:@"="];
503 if (splitOptRange.location != NSNotFound)
505 /* Split thisOpt into an optName setting an optValue. */
506 optName = [thisOpt substringToIndex:splitOptRange.location];
507 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
509 /*Run through the available widgets for x264 opts and set them, as you add widgets,
510 they need to be added here. This should be moved to its own method probably*/
512 /*bframes NSPopUpButton*/
513 if ([optName isEqualToString:@"bframes"])
515 [fX264optBframesPopUp selectItemAtIndex:[optValue intValue]+1];
517 /*ref NSPopUpButton*/
518 if ([optName isEqualToString:@"ref"])
520 [fX264optRefPopUp selectItemAtIndex:[optValue intValue]+1];
522 /*No Fast PSkip NSButton*/
523 if ([optName isEqualToString:@"no-fast-pskip"])
525 [fX264optNfpskipSwitch setState:[optValue intValue]];
527 /*No Dict Decimate NSButton*/
528 if ([optName isEqualToString:@"no-dct-decimate"])
530 [fX264optNodctdcmtSwitch setState:[optValue intValue]];
532 /*Sub Me NSPopUpButton*/
533 if ([optName isEqualToString:@"subq"])
535 [fX264optSubmePopUp selectItemAtIndex:[optValue intValue]+1];
537 /*Trellis NSPopUpButton*/
538 if ([optName isEqualToString:@"trellis"])
540 [fX264optTrellisPopUp selectItemAtIndex:[optValue intValue]+1];
542 /*Mixed Refs NSButton*/
543 if ([optName isEqualToString:@"mixed-refs"])
545 [fX264optMixedRefsSwitch setState:[optValue intValue]];
547 /*Motion Estimation NSPopUpButton*/
548 if ([optName isEqualToString:@"me"])
550 if ([optValue isEqualToString:@"dia"])
551 [fX264optMotionEstPopUp selectItemAtIndex:1];
552 else if ([optValue isEqualToString:@"hex"])
553 [fX264optMotionEstPopUp selectItemAtIndex:2];
554 else if ([optValue isEqualToString:@"umh"])
555 [fX264optMotionEstPopUp selectItemAtIndex:3];
556 else if ([optValue isEqualToString:@"esa"])
557 [fX264optMotionEstPopUp selectItemAtIndex:4];
559 /*ME Range NSPopUpButton*/
560 if ([optName isEqualToString:@"merange"])
562 [fX264optMERangePopUp selectItemAtIndex:[optValue intValue]-3];
564 /*Weighted B-Frames NSButton*/
565 if ([optName isEqualToString:@"weightb"])
567 [fX264optWeightBSwitch setState:[optValue intValue]];
569 /*B Pyramid NSPButton*/
570 if ([optName isEqualToString:@"b-pyramid"])
572 [fX264optBPyramidSwitch setState:[optValue intValue]];
574 /*Direct B-frame Prediction NSPopUpButton*/
575 if ([optName isEqualToString:@"direct"])
577 if ([optValue isEqualToString:@"none"])
578 [fX264optDirectPredPopUp selectItemAtIndex:1];
579 else if ([optValue isEqualToString:@"spatial"])
580 [fX264optDirectPredPopUp selectItemAtIndex:2];
581 else if ([optValue isEqualToString:@"temporal"])
582 [fX264optDirectPredPopUp selectItemAtIndex:3];
583 else if ([optValue isEqualToString:@"auto"])
584 [fX264optDirectPredPopUp selectItemAtIndex:4];
586 /*Deblocking NSPopUpButtons*/
587 if ([optName isEqualToString:@"deblock"])
589 NSString * alphaDeblock = @"";
590 NSString * betaDeblock = @"";
592 NSRange splitDeblock = [optValue rangeOfString:@","];
593 alphaDeblock = [optValue substringToIndex:splitDeblock.location];
594 betaDeblock = [optValue substringFromIndex:splitDeblock.location + 1];
596 if ([alphaDeblock isEqualToString:@"0"] && [betaDeblock isEqualToString:@"0"])
598 /* When both filters are at 0, default */
599 [fX264optAlphaDeblockPopUp selectItemAtIndex:0];
600 [fX264optBetaDeblockPopUp selectItemAtIndex:0];
604 if (![alphaDeblock isEqualToString:@"0"])
606 /* Alpha isn't 0, so set it. The offset of 7 is
607 because filters start at -6 instead of at 0. */
608 [fX264optAlphaDeblockPopUp selectItemAtIndex:[alphaDeblock intValue]+7];
612 /* Set alpha filter to 0, which is 7 up
613 because filters start at -6, not 0. */
614 [fX264optAlphaDeblockPopUp selectItemAtIndex:7];
617 if (![betaDeblock isEqualToString:@"0"])
619 /* Beta isn't 0, so set it. */
620 [fX264optBetaDeblockPopUp selectItemAtIndex:[betaDeblock intValue]+7];
624 /* Set beta filter to 0. */
625 [fX264optBetaDeblockPopUp selectItemAtIndex:7];
629 /* Analysis NSPopUpButton */
630 if ([optName isEqualToString:@"analyse"])
632 if ([optValue isEqualToString:@"p8x8,b8x8,i8x8,i4x4"])
634 /* Default ("some") */
635 [fX264optAnalysePopUp selectItemAtIndex:0];
637 if ([optValue isEqualToString:@"none"])
639 [fX264optAnalysePopUp selectItemAtIndex:1];
641 if ([optValue isEqualToString:@"all"])
643 [fX264optAnalysePopUp selectItemAtIndex:2];
646 /* 8x8 DCT NSButton */
647 if ([optName isEqualToString:@"8x8dct"])
649 [fX264opt8x8dctSwitch setState:[optValue intValue]];
652 if ([optName isEqualToString:@"cabac"])
654 [fX264optCabacSwitch setState:[optValue intValue]];
662 * Resets the option string to mirror the GUI widgets.
664 - (IBAction) X264AdvancedOptionsChanged: (id) sender
666 /*Determine which outlet is being used and set optName to process accordingly */
667 NSString * optNameToChange = @""; // The option name such as "bframes"
669 if (sender == fX264optBframesPopUp)
671 optNameToChange = @"bframes";
673 if (sender == fX264optRefPopUp)
675 optNameToChange = @"ref";
677 if (sender == fX264optNfpskipSwitch)
679 optNameToChange = @"no-fast-pskip";
681 if (sender == fX264optNodctdcmtSwitch)
683 optNameToChange = @"no-dct-decimate";
685 if (sender == fX264optSubmePopUp)
687 optNameToChange = @"subq";
689 if (sender == fX264optTrellisPopUp)
691 optNameToChange = @"trellis";
693 if (sender == fX264optMixedRefsSwitch)
695 optNameToChange = @"mixed-refs";
697 if (sender == fX264optMotionEstPopUp)
699 optNameToChange = @"me";
701 if (sender == fX264optMERangePopUp)
703 optNameToChange = @"merange";
705 if (sender == fX264optWeightBSwitch)
707 optNameToChange = @"weightb";
709 if (sender == fX264optBPyramidSwitch)
711 optNameToChange = @"b-pyramid";
713 if (sender == fX264optDirectPredPopUp)
715 optNameToChange = @"direct";
717 if (sender == fX264optAlphaDeblockPopUp)
719 optNameToChange = @"deblock";
721 if (sender == fX264optBetaDeblockPopUp)
723 optNameToChange = @"deblock";
725 if (sender == fX264optAnalysePopUp)
727 optNameToChange = @"analyse";
729 if (sender == fX264opt8x8dctSwitch)
731 optNameToChange = @"8x8dct";
733 if (sender == fX264optCabacSwitch)
735 optNameToChange = @"cabac";
738 /* Set widgets depending on the opt string in field */
739 NSString * thisOpt; // The separated option such as "bframes=3"
740 NSString * optName = @""; // The option name such as "bframes"
741 NSString * optValue = @"";// The option value such as "3"
742 NSArray *currentOptsArray;
744 /*First, we get an opt string to process */
745 NSString *currentOptString = [fDisplayX264Options stringValue];
747 /* There are going to be a few possibilities.
748 - The option might start off the string.
749 - The option might be in the middle of the string.
750 - The option might not be in the string at all yet.
751 - The string itself might not yet exist.
753 Because each of these possibilities means constructing a different kind of string,
754 they're all handled separately in a sea of messy, somewhat redundant code. =( */
756 /* If the option is in the string but not the beginning of it, it will be in the form of ":optName=value"
757 so we really want to be looking for ":optNameToChange=" rather than "optNameToChange". */
758 NSString *checkOptNameToChange = [NSString stringWithFormat:@":%@=",optNameToChange];
760 /* Now we store the part of the string up through the option name in currentOptRange. */
761 NSRange currentOptRange = [currentOptString rangeOfString:checkOptNameToChange];
763 /* We need to know if the option is at the beginning of the string.
764 If it is at the start, it won't be preceded by a colon.
765 To figure this out, we'll use the rangeOfString method. First,
766 store what the option name would be if if it was at the beginning,
767 in checkOptNameToChangeBeginning. Then, find its range in the string.
768 If the range is 0, it's the first option listed in the string. */
769 NSString *checkOptNameToChangeBeginning = [NSString stringWithFormat:@"%@=",optNameToChange];
770 NSRange currentOptRangeBeginning = [currentOptString rangeOfString:checkOptNameToChangeBeginning];
772 if (currentOptRange.location != NSNotFound || currentOptRangeBeginning.location == 0)
774 /* If the option is in the string wth a semicolon, or starts the string, it's time to edit.
775 This means parsing the whole string into an array of options and values. From there,
776 iterate through the options, and when you reach the one that's been changed, edit it. */
778 /* Create new empty opt string*/
779 NSString *changedOptString = @"";
781 /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/
782 currentOptsArray = [currentOptString componentsSeparatedByString:@":"];
784 /*iterate through the array and get <opts> and <values*/
786 int currentOptsArrayCount = [currentOptsArray count];
787 for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)
789 thisOpt = [currentOptsArray objectAtIndex:loopcounter];
790 NSRange splitOptRange = [thisOpt rangeOfString:@"="];
792 if (splitOptRange.location != NSNotFound)
794 /* First off, it's time to handle option strings that
795 already have at least one option=value pair in them. */
797 optName = [thisOpt substringToIndex:splitOptRange.location];
798 optValue = [thisOpt substringFromIndex:splitOptRange.location + 1];
800 /*Run through the available widgets for x264 opts and set them, as you add widgets,
801 they need to be added here. This should be moved to its own method probably*/
803 /*If the optNameToChange is found, appropriately change the value or delete it if
804 "Unspecified" is set.*/
805 if ([optName isEqualToString:optNameToChange])
807 if ([optNameToChange isEqualToString:@"deblock"])
809 if ((([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optAlphaDeblockPopUp indexOfSelectedItem] == 7)) && (([fX264optBetaDeblockPopUp indexOfSelectedItem] == 0) || ([fX264optBetaDeblockPopUp indexOfSelectedItem] == 7)))
811 /* When both deblock widgets are 0 or default or a mix of the two,
812 use a blank string, since deblocking defaults to 0,0. */
817 /* Otherwise the format is deblock=a,b, where a and b both have an array
818 offset of 7 because deblocking values start at -6 instead of at zero. */
819 thisOpt = [NSString stringWithFormat:@"%@=%d,%d",optName, ([fX264optAlphaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optAlphaDeblockPopUp indexOfSelectedItem]-7 : 0,([fX264optBetaDeblockPopUp indexOfSelectedItem] != 0) ? [fX264optBetaDeblockPopUp indexOfSelectedItem]-7 : 0];
822 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"] )
824 /* Here is where we take care of the boolean options that work overtly:
825 no-dct-decimate being on means no-dct-decimate=1, etc. Some options
826 require the inverse, but those will be handled a couple lines down. */
827 if ([sender state] == 0)
829 /* When these options are false, don't include them. They all default
830 to being set off, so they don't need to be mentioned at all. */
835 /* Otherwise, include them as optioname=1 */
836 thisOpt = [NSString stringWithFormat:@"%@=%d",optName,1];
839 else if ([optNameToChange isEqualToString:@"cabac"])
841 /* CABAC is odd, in that it defaults to being on. That means
842 it only needs to be included in the string when turned off. */
843 if ([sender state] == 1)
845 /* It's true so don't include it. */
850 /* Otherwise, include cabac=0 in the string to enable CAVLC. */
851 thisOpt = [NSString stringWithFormat:@"%@=%d",optName,0];
854 else if (([sender indexOfSelectedItem] == 0) && (sender != fX264optAlphaDeblockPopUp) && (sender != fX264optBetaDeblockPopUp) ) // means that "unspecified" is chosen, lets then remove it from the string
856 /* When a widget is at index 0, it's default. Default means don't add to the string.
857 The exception for deblocking is because for those, *both* need to at index 0
858 for it to default, so it's handled separately, above this section. */
861 else if ([optNameToChange isEqualToString:@"me"])
863 /* Motion estimation uses string values, so this switch
864 pairs the widget index with the right value string. */
865 switch ([sender indexOfSelectedItem])
868 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"dia"];
872 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"hex"];
876 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"umh"];
880 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"esa"];
887 else if ([optNameToChange isEqualToString:@"direct"])
889 /* Direct prediction uses string values, so this switch
890 pairs the right string value with the right widget index. */
891 switch ([sender indexOfSelectedItem])
894 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
898 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"spatial"];
902 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"temporal"];
906 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"auto"];
913 else if ([optNameToChange isEqualToString:@"analyse"])
915 /* Analysis uses string values as well. */
916 switch ([sender indexOfSelectedItem])
919 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"none"];
923 thisOpt = [NSString stringWithFormat:@"%@=%@",optName,@"all"];
930 else if ([optNameToChange isEqualToString:@"merange"])
932 /* Motion estimation range uses an odd array offset because in addition
933 to starting with index 0 as default, index 1 starts at 4 instead of 1,
934 because merange can't go below 4. So it has to be handled separately. */
935 thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]+3];
937 else // we have a valid value to change, so change it
939 if ( [sender indexOfSelectedItem] != 0 )
940 /* Here's our general case, that catches things like ref frames and b-frames.
941 Basically, any options that are PopUp menus with index 0 as default and
942 index 1 as 1, with numerical values, are all handled right here. All of
943 the above stuff is for the exceptions to the general case. */
944 thisOpt = [NSString stringWithFormat:@"%@=%d",optName,[sender indexOfSelectedItem]-1];
949 /* Construct New String for opts here */
950 if ([thisOpt isEqualToString:@""])
952 /* Blank option, so just add it to the string. (Why?) */
953 changedOptString = [NSString stringWithFormat:@"%@%@",changedOptString,thisOpt];
957 if ([changedOptString isEqualToString:@""])
959 /* No existing string, make the string this option. */
960 changedOptString = [NSString stringWithFormat:@"%@",thisOpt];
964 /* Existing string, existing option. Append the
965 option to the string, preceding it with a colon. */
966 changedOptString = [NSString stringWithFormat:@"%@:%@",changedOptString,thisOpt];
971 /* Change the dislayed option string to reflect the new modified settings */
972 [fDisplayX264Options setStringValue:[NSString stringWithFormat:changedOptString]];
974 else // if none exists, add it to the string
976 /* This is where options that aren't already in the string are handled. */
977 if ([[fDisplayX264Options stringValue] isEqualToString: @""])
979 /* The option might not be in the string because the
980 string is empty. Handle this possibility first. */
981 if ([optNameToChange isEqualToString:@"me"])
983 /* Special case for motion estimation, which uses string values
984 that need to be paired up with the equivalent widget index. */
985 switch ([sender indexOfSelectedItem])
988 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
989 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"dia"]]];
993 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
994 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"hex"]]];
998 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
999 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"umh"]]];
1003 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1004 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"esa"]]];
1011 else if ([optNameToChange isEqualToString:@"direct"])
1013 /* Special case for direct prediction, which uses string values
1014 that need to be paired up with the equivalent widget index. */
1015 switch ([sender indexOfSelectedItem])
1018 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1019 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
1023 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1024 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"spatial"]]];
1028 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1029 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"temporal"]]];
1033 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1034 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"auto"]]];
1041 else if ([optNameToChange isEqualToString:@"analyse"])
1043 /* Special case for partition analysis, which uses string values
1044 that need to be paired up with the equivalent widget index. */
1045 switch ([sender indexOfSelectedItem])
1048 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1049 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
1053 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1054 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"all"]]];
1062 else if ([optNameToChange isEqualToString:@"merange"])
1064 /* Special case for motion estimation range, which uses
1065 a widget index offset of 3. This is because the
1066 first valid value after default is four, not zero. */
1067 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1068 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]+3]]];
1070 else if ([optNameToChange isEqualToString:@"deblock"])
1072 /* Very special case for deblock. Uses a weird widget index offset
1073 of 7, because the first value after default is -6, rather than 0.
1074 As well, deblock only goes to default when *both* alpha and beta
1075 are zero. If only one is zero, you can't mark it down as default.
1076 Instead, mark that one down as literally 0. This is because when
1077 widgets are at default values, they aren't included in the string.
1078 If only one filter is at 0, both need to be overtly specified. */
1079 [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]]];
1081 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"] )
1083 /* This covers all the boolean options that need to be specified only when true. */
1084 if ([sender state] == 0)
1086 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@""]];
1090 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1091 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];
1094 else if ([optNameToChange isEqualToString:@"cabac"])
1096 /* CABAC is weird in that you need the inverse. Only include in the string
1097 when cabac=0, because cabac=1 is the default. Turning it off means CAVLC. */
1098 if ([sender state] == 1)
1100 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@""]];
1104 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1105 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];
1110 if ( [sender indexOfSelectedItem] != 0 )
1111 /* General case to cover all the normal PopUp widgets, like ref and b-frames. */
1112 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@=%@",
1113 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]-1]]];
1118 /* The string isn't empty, and the option isn't already in it,
1119 so it will need to be appended to the string with a colon. */
1120 if ([optNameToChange isEqualToString:@"me"])
1122 /* Special case for motion estimation, which uses string values
1123 that need to be paired up with the equivalent widget index. */
1124 switch ([sender indexOfSelectedItem])
1127 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1128 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1129 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"dia"]]];
1133 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1134 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1135 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"hex"]]];
1139 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1140 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1141 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"umh"]]];
1145 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1146 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1147 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"esa"]]];
1154 else if ([optNameToChange isEqualToString:@"direct"])
1156 /* Special case for direct prediction, which uses string values
1157 that need to be paired up with the equivalent widget index. */
1158 switch ([sender indexOfSelectedItem])
1161 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1162 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1163 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
1167 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1168 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1169 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"spatial"]]];
1173 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1174 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1175 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"temporal"]]];
1179 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1180 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1181 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"auto"]]];
1188 else if ([optNameToChange isEqualToString:@"analyse"])
1190 /* Special case for partition analysis, which uses string values
1191 that need to be paired up with the equivalent widget index. */
1192 switch ([sender indexOfSelectedItem])
1195 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1196 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1197 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"none"]]];
1201 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",
1202 [NSString stringWithFormat:[fDisplayX264Options stringValue]],
1203 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"all"]]];
1211 else if ([optNameToChange isEqualToString:@"merange"])
1213 /* Motion estimation range uses a weird offset since its index goes
1214 0: default, 1: 4, because the first valid value is 4, not 1. */
1215 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]],
1216 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]+3]]];
1218 else if ([optNameToChange isEqualToString:@"deblock"])
1220 /* Deblock is really weird because it has two values, and if only one is default, both
1221 still need to be specified directly. with the default one at zero. To make deblock
1222 just a little more fun, values start at -6 instead of at zero. */
1223 [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]]];
1225 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"] )
1227 /* Covers all the normal booleans, that only need to be included in the string when they're true. */
1228 if ([sender state] == 0)
1230 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]]]];
1234 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]],
1235 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];
1238 else if ([optNameToChange isEqualToString:@"cabac"])
1240 /* CABAC is weird, in that it's an inverse. Only include it in the string when it's false. */
1241 if ([sender state] == 1)
1243 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]]]];
1247 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]],
1248 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender state]]]];
1253 /* General case to handle the normal PopUp widgets like ref and b-frames. */
1254 if ( [sender indexOfSelectedItem] != 0 )
1255 [fDisplayX264Options setStringValue:[NSString stringWithFormat:@"%@:%@=%@",[NSString stringWithFormat:[fDisplayX264Options stringValue]],
1256 [NSString stringWithFormat:optNameToChange],[NSString stringWithFormat:@"%d",[sender indexOfSelectedItem]-1]]];
1261 /* We now need to reset the opt widgets since we changed some stuff */
1262 [self X264AdvancedOptionsSet:sender];