OSDN Git Service

Reverting 3452
[handbrake-jp/handbrake-jp-git.git] / win / C# / Controls / x264Panel.cs
1 /*  x264Panel.cs $\r
2     This file is part of the HandBrake source code.\r
3     Homepage: <http://handbrake.fr>.\r
4     It may be used under the terms of the GNU General Public License. */\r
5 \r
6 namespace Handbrake.Controls\r
7 {\r
8     using System;\r
9     using System.Windows.Forms;\r
10 \r
11     public partial class x264Panel : UserControl\r
12     {\r
13         /* \r
14          * TODO This code was ported from the obj-c MacGUI code. It's really messy and could really do with being cleaned up\r
15          * at some point.\r
16          */\r
17 \r
18         /// <summary>\r
19         /// Initializes a new instance of the <see cref="x264Panel"/> class. \r
20         /// Initializes a new instance of the x264 panel user control\r
21         /// </summary>\r
22         public x264Panel()\r
23         {\r
24             InitializeComponent();\r
25 \r
26             if (Properties.Settings.Default.tooltipEnable)\r
27                 ToolTip.Active = true;\r
28 \r
29             Reset2Defaults();\r
30         }\r
31 \r
32         /// <summary>\r
33         /// Gets or sets the X264 query string\r
34         /// </summary>\r
35         public string X264Query\r
36         {\r
37             get\r
38             {\r
39                 return rtf_x264Query.Text;\r
40             }\r
41             set { rtf_x264Query.Text = value; }\r
42         }\r
43 \r
44         /// <summary>\r
45         /// Reset all components to defaults and clears the x264 rtf box\r
46         /// </summary>\r
47         public void Reset2Defaults()\r
48         {\r
49             check_8x8DCT.CheckState = CheckState.Checked;\r
50             check_Cabac.CheckState = CheckState.Checked;\r
51             check_weightp.CheckState = CheckState.Checked;\r
52             check_noDCTDecimate.CheckState = CheckState.Unchecked;\r
53             combo_pyrmidalBFrames.SelectedIndex = 0;\r
54             drop_analysis.SelectedIndex = 0;\r
55             drop_bFrames.SelectedIndex = 0;\r
56             drop_deblockAlpha.SelectedIndex = 0;\r
57             drop_deblockBeta.SelectedIndex = 0;\r
58             drop_directPrediction.SelectedIndex = 0;\r
59             drop_MotionEstimationMethod.SelectedIndex = 0;\r
60             drop_MotionEstimationRange.SelectedIndex = 0;\r
61             drop_refFrames.SelectedIndex = 0;\r
62             drop_subpixelMotionEstimation.SelectedIndex = 0;\r
63             drop_trellis.SelectedIndex = 0;\r
64             slider_psyrd.Value = 10;\r
65             slider_psytrellis.Value = 0;\r
66             drop_adaptBFrames.SelectedIndex = 0;\r
67 \r
68             rtf_x264Query.Text = string.Empty;\r
69         }\r
70 \r
71         #region Standardize Option String\r
72         /// <summary>\r
73         /// Iterate over every x264 option, standardize it, write the full string to the x264 rtf box\r
74         /// </summary>\r
75         public void StandardizeOptString()\r
76         {\r
77             /* Set widgets depending on the opt string in field */\r
78             string thisOpt; // The separated option such as "bframes=3"\r
79             string optName; // The option name such as "bframes"\r
80             string optValue; // The option value such as "3"\r
81             string changedOptString = string.Empty;\r
82             string[] currentOptsArray;\r
83 \r
84             /*First, we get an opt string to process */\r
85             string currentOptString = rtf_x264Query.Text;\r
86 \r
87             /*verify there is an opt string to process */\r
88             if (currentOptString.Contains("="))\r
89             {\r
90                 /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/\r
91                 currentOptsArray = currentOptString.Split(':');\r
92 \r
93                 /*iterate through the array and get <opts> and <values*/\r
94                 int loopcounter;\r
95                 int currentOptsArrayCount = currentOptsArray.Length;\r
96                 for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)\r
97                 {\r
98                     thisOpt = currentOptsArray[loopcounter];\r
99                     if (currentOptsArray[currentOptsArrayCount - 1] == string.Empty)\r
100                         break;\r
101 \r
102                     string[] splitOptRange = thisOpt.Split('=');\r
103                     if (thisOpt != string.Empty)\r
104                     {\r
105                         if (thisOpt.Contains("="))\r
106                         {\r
107                             optName = splitOptRange[0];\r
108                             optValue = splitOptRange[1];\r
109 \r
110                             /* Standardize the names here depending on whats in the string */\r
111                             optName = StandardizeOptName(optName);\r
112                             thisOpt = optName + "=" + optValue;\r
113                         }\r
114                         else // No value given so we use a default of "1"\r
115                         {\r
116                             optName = thisOpt;\r
117                             /* Standardize the names here depending on whats in the string */\r
118                             optName = StandardizeOptName(optName);\r
119                             thisOpt = optName + "=1";\r
120                         }\r
121                     }\r
122 \r
123                     /* Construct New String for opts here */\r
124                     if (thisOpt == string.Empty)\r
125                         changedOptString = changedOptString + thisOpt;\r
126                     else\r
127                     {\r
128                         if (changedOptString == string.Empty)\r
129                             changedOptString = thisOpt;\r
130                         else\r
131                             changedOptString = changedOptString + ":" + thisOpt;\r
132                     }\r
133                 }\r
134             }\r
135 \r
136             /* Change the option string to reflect the new standardized option string */\r
137             if (changedOptString != string.Empty)\r
138                 rtf_x264Query.Text = changedOptString;\r
139         }\r
140 \r
141         /// <summary>\r
142         /// Take a single option and standardize it. Returns as a String\r
143         /// Input: String. - Single X264 Option. Name only\r
144         /// Output: String - Single X264 Option. Name only. Changed to standard format\r
145         /// </summary>\r
146         /// <param name="cleanOptNameString">a string of x264 options to clean</param>\r
147         /// <returns>A string containing standardized x264 option names</returns>\r
148         private static string StandardizeOptName(string cleanOptNameString)\r
149         {\r
150             string input = cleanOptNameString;\r
151 \r
152             /* Reference Frames */\r
153             if (input.Equals("ref") || input.Equals("frameref"))\r
154                 cleanOptNameString = "ref";\r
155 \r
156             /*No Dict Decimate*/\r
157             if (input.Equals("no-dct-decimate") || input.Equals("no_dct_decimate") || input.Equals("nodct_decimate"))\r
158                 cleanOptNameString = "no-dct-decimate";\r
159 \r
160             /*Subme*/\r
161             if (input.Equals("subme"))\r
162                 cleanOptNameString = "subq";\r
163 \r
164             /*ME Range*/\r
165             if (input.Equals("me-range") || input.Equals("me_range"))\r
166                 cleanOptNameString = "merange";\r
167 \r
168             /*B Pyramid*/\r
169             if (input.Equals("b_pyramid"))\r
170                 cleanOptNameString = "b-pyramid";\r
171 \r
172             /*Direct Prediction*/\r
173             if (input.Equals("direct-pred") || input.Equals("direct_pred"))\r
174                 cleanOptNameString = "direct";\r
175 \r
176             /*Deblocking*/\r
177             if (input.Equals("filter"))\r
178                 cleanOptNameString = "deblock";\r
179 \r
180             /*Analysis*/\r
181             if (input.Equals("partitions"))\r
182                 cleanOptNameString = "analyse";\r
183 \r
184             return cleanOptNameString;\r
185         }\r
186         #endregion\r
187 \r
188         /// <summary>\r
189         /// Resets the GUI widgets to the contents of the option string.\r
190         /// </summary>\r
191         public void SetCurrentSettingsInPanel()\r
192         {\r
193             /* Set widgets depending on the opt string in field */\r
194             string thisOpt; // The separated option such as "bframes=3"\r
195             string optName; // The option name such as "bframes"\r
196             string optValue; // The option value such as "3"\r
197             string[] currentOptsArray;\r
198 \r
199             // Set currentOptString to the contents of the text box.\r
200             string currentOptString = rtf_x264Query.Text.Replace("\n", string.Empty);\r
201 \r
202             /*verify there is an opt string to process */\r
203             if (currentOptString.Contains("="))\r
204             {\r
205                 /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/\r
206                 currentOptsArray = currentOptString.Split(':');\r
207 \r
208                 /*iterate through the array and get <opts> and <values*/\r
209                 int loopcounter;\r
210                 int currentOptsArrayCount = currentOptsArray.Length;\r
211 \r
212                 /*iterate through the array and get <opts> and <values*/\r
213                 for (loopcounter = 0; loopcounter < currentOptsArrayCount; loopcounter++)\r
214                 {\r
215                     thisOpt = currentOptsArray[loopcounter];\r
216                     string[] splitOptRange = thisOpt.Split('=');\r
217 \r
218                     if (thisOpt.Contains("="))\r
219                     {\r
220                         optName = splitOptRange[0];\r
221                         optValue = splitOptRange[1];\r
222 \r
223                         /*Run through the available widgets for x264 opts and set them, as you add widgets, \r
224                             they need to be added here. This should be moved to its own method probably*/\r
225                         switch (optName)\r
226                         {\r
227                             case "bframes":\r
228                                 drop_bFrames.SelectedItem = optValue;\r
229                                 continue;\r
230                             case "ref":\r
231                                 drop_refFrames.SelectedItem = optValue;\r
232                                 continue;\r
233                             case "weightp":\r
234                                 this.check_weightp.CheckState = optValue == "0" ? CheckState.Unchecked : CheckState.Checked;\r
235                                 continue;\r
236                             case "no-dct-decimate":\r
237                                 check_noDCTDecimate.CheckState = CheckState.Checked;\r
238                                 continue;\r
239                             case "subq":\r
240                                 int subqValue;\r
241                                 if (int.TryParse(optValue, out subqValue))\r
242                                 {\r
243                                     drop_subpixelMotionEstimation.SelectedIndex = subqValue + 1;\r
244                                 }\r
245                                 continue;\r
246                             case "trellis":\r
247                                 switch (optValue)\r
248                                 {\r
249                                     case "0":\r
250                                         drop_trellis.SelectedIndex = 1;\r
251                                         break;\r
252                                     case "1":\r
253                                          drop_trellis.SelectedIndex = 2;\r
254                                         break;\r
255                                     case "2":\r
256                                         drop_trellis.SelectedIndex = 3;\r
257                                         break;\r
258                                 }\r
259                                 continue;\r
260                             case "me":\r
261                                 if (optValue.Equals("dia"))\r
262                                     drop_MotionEstimationMethod.SelectedItem = "Diamond";\r
263                                 else if (optValue.Equals("hex"))\r
264                                     drop_MotionEstimationMethod.SelectedItem = "Hexagon";\r
265                                 else if (optValue.Equals("umh"))\r
266                                     drop_MotionEstimationMethod.SelectedItem = "Uneven Multi-Hexagon";\r
267                                 else if (optValue.Equals("esa"))\r
268                                     drop_MotionEstimationMethod.SelectedItem = "Exhaustive";\r
269                                 else if (optValue.Equals("tesa"))\r
270                                     drop_MotionEstimationMethod.SelectedItem = "Transformed Exhaustive";\r
271                                 continue;\r
272                             case "merange":\r
273                                 drop_MotionEstimationRange.SelectedItem = optValue;\r
274                                 continue;\r
275                             case "b-adapt":\r
276                                 int badapt;\r
277                                 int.TryParse(optValue, out badapt);\r
278                                 drop_adaptBFrames.SelectedIndex = (badapt + 1);\r
279                                 continue;\r
280                             case "b-pyramid":\r
281                                 switch (optValue)\r
282                                 {\r
283                                     case "normal":\r
284                                         combo_pyrmidalBFrames.SelectedIndex = 0;\r
285                                         break;\r
286                                     case "strict":\r
287                                         combo_pyrmidalBFrames.SelectedIndex = 2;\r
288                                         break;\r
289                                     case "none":\r
290                                         combo_pyrmidalBFrames.SelectedIndex = 1;\r
291                                         break;\r
292                                 }\r
293                                 continue;\r
294                             case "direct":\r
295                                 if (optValue == "auto")\r
296                                     optValue = "Automatic";\r
297 \r
298                                 if (optValue != string.Empty)\r
299                                 {\r
300                                     char[] letters = optValue.ToCharArray();\r
301                                     letters[0] = Char.ToUpper(letters[0]);\r
302                                     optValue = new string(letters);\r
303                                 }\r
304 \r
305                                 drop_directPrediction.SelectedItem = optValue;\r
306                                 continue;\r
307                             case "deblock":\r
308                                 string[] splitDeblock = optValue.Split(',');\r
309                                 string alphaDeblock = splitDeblock[0];\r
310                                 string betaDeblock = splitDeblock[1];\r
311 \r
312                                 if (alphaDeblock.Equals("0") && betaDeblock.Replace("\n", string.Empty).Equals("0"))\r
313                                 {\r
314                                     drop_deblockAlpha.SelectedItem = "Default (0)";\r
315                                     drop_deblockBeta.SelectedItem = "Default (0)";\r
316                                 }\r
317                                 else\r
318                                 {\r
319                                     drop_deblockAlpha.SelectedItem = !alphaDeblock.Equals("0") ? alphaDeblock : "0";\r
320 \r
321                                     drop_deblockBeta.SelectedItem = !betaDeblock.Replace("\n", string.Empty).Equals("0")\r
322                                                                         ? betaDeblock.Replace("\n", string.Empty)\r
323                                                                         : "0";\r
324                                 }\r
325                                 continue;\r
326                             case "analyse":\r
327                                 if (optValue.Equals("p8x8,b8x8,i8x8,i4x4"))\r
328                                     drop_analysis.SelectedItem = "Default (most)";\r
329                                 if (optValue.Equals("none"))\r
330                                     drop_analysis.SelectedItem = "None";\r
331                                 if (optValue.Equals("i4x4,i8x8"))\r
332                                     drop_analysis.SelectedItem = "Some";\r
333                                 if (optValue.Equals("all"))\r
334                                     drop_analysis.SelectedItem = "All";\r
335                                 continue;\r
336                             case "8x8dct":\r
337                                 check_8x8DCT.CheckState = optValue == "1" ? CheckState.Checked : CheckState.Unchecked;\r
338                                 continue;\r
339                             case "aq-strength":\r
340                                 float value;\r
341                                 float.TryParse(optValue, out value);\r
342                                 int sliderValue;\r
343                                 int.TryParse((value * 10).ToString(), out sliderValue);\r
344                                 slider_adaptiveQuantStrength.Value = sliderValue;\r
345                                 continue;\r
346                             case "cabac":\r
347                                 check_Cabac.CheckState = CheckState.Unchecked;\r
348                                 continue;\r
349                             case "psy-rd":\r
350                                 string[] x = optValue.Split(',');\r
351 \r
352                                 double psyrd, psytrellis;\r
353                                 int val, val2;\r
354 \r
355                                 // default psy-rd = 1 (10 for the slider)\r
356                                 psyrd = double.TryParse(x[0], out psyrd) ? psyrd * 10 : 10.0;\r
357                                 // default psy-trellis = 0\r
358                                 psytrellis = double.TryParse(x[1], out psytrellis) ? psytrellis * 20 : 0.0;\r
359 \r
360                                 int.TryParse(psyrd.ToString(), out val);\r
361                                 int.TryParse(psytrellis.ToString(), out val2);\r
362 \r
363                                 slider_psyrd.Value = val;\r
364                                 slider_psytrellis.Value = val2;\r
365                                 continue;\r
366                         }\r
367                     }\r
368                 }\r
369             }\r
370         }\r
371 \r
372         /// <summary>\r
373         /// This function will update the X264 Query when one of the GUI widgets changes.\r
374         /// </summary>\r
375         /// <param name="sender">\r
376         /// The sender.\r
377         /// </param>\r
378         private void OnX264WidgetChange(string sender)\r
379         {\r
380             Animate(sender);\r
381             string optNameToChange = sender;\r
382             string currentOptString = rtf_x264Query.Text;\r
383 \r
384             /*First, we create a pattern to check for ":"optNameToChange"=" to modify the option if the name falls after\r
385                 the first character of the opt string (hence the ":") */\r
386             string checkOptNameToChange = ":" + optNameToChange + "=";\r
387             string checkOptNameToChangeBegin = optNameToChange + "=";\r
388 \r
389             // IF the current H264 Option String Contains Multiple Items or Just 1 Item\r
390             if ((currentOptString.Contains(checkOptNameToChange)) || (currentOptString.StartsWith(checkOptNameToChangeBegin)))\r
391                 HasOptions(currentOptString, optNameToChange);\r
392             else // IF there is no options in the rich text box!\r
393                 HasNoOptions(optNameToChange);\r
394         }\r
395 \r
396         /// <summary>\r
397         /// Called when the current x264 option string contains multiple (or a single) item(s) in it seperated by :\r
398         /// it updates the current option that the widget corrosponds to, if it is already in thes string.\r
399         /// </summary>\r
400         /// <param name="currentOptString">The Current Option String</param>\r
401         /// <param name="optNameToChange">Name of the option to change</param>\r
402         private void HasOptions(string currentOptString, string optNameToChange)\r
403         {\r
404             string thisOpt; // The separated option such as "bframes=3"\r
405             string optName; // The option name such as "bframes"\r
406             string[] currentOptsArray;\r
407 \r
408             /* Create new empty opt string*/\r
409             string changedOptString = string.Empty;\r
410 \r
411             /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/\r
412             currentOptsArray = currentOptString.Split(':');\r
413 \r
414             /*iterate through the array and get <opts> and <values*/\r
415             for (int loopcounter = 0; loopcounter < currentOptsArray.Length; loopcounter++)\r
416             {\r
417                 thisOpt = currentOptsArray[loopcounter];\r
418 \r
419                 if (thisOpt.Contains("="))\r
420                 {\r
421                     string[] splitOptRange = thisOpt.Split('=');\r
422 \r
423                     optName = splitOptRange[0]; // e.g bframes\r
424 \r
425                     /* \r
426                      * Run through the available widgets for x264 opts and set them, as you add widgets,\r
427                      * they need to be added here. This should be moved to its own method probably\r
428                      * If the optNameToChange is found, appropriately change the value or delete it if\r
429                      * "unspecified" is set.\r
430                      */\r
431                     if (optName.Equals(optNameToChange))\r
432                     {\r
433                         if (optNameToChange.Equals("deblock"))\r
434                         {\r
435                             string da = drop_deblockAlpha.SelectedItem.ToString();\r
436                             string db = drop_deblockBeta.SelectedItem.ToString();\r
437 \r
438                             if (((da.Contains("Default")) && (db.Contains("Default"))) ||\r
439                                 ((da.Contains("0")) && (db.Contains("0"))))\r
440                             {\r
441                                 drop_deblockBeta.SelectedItem = "Default (0)";\r
442                                 drop_deblockAlpha.SelectedItem = "Default (0)";\r
443                                 thisOpt = string.Empty;\r
444                             }\r
445                             else if ((!da.Contains("Default")) && (db.Contains("Default")))\r
446                             {\r
447                                 drop_deblockBeta.SelectedItem = "0";\r
448                                 thisOpt = "deblock=" + da + ",0";\r
449                             }\r
450                             else if ((da.Contains("Default")) && (!db.Contains("Default")))\r
451                             {\r
452                                 drop_deblockAlpha.SelectedItem = "0";\r
453                                 thisOpt = "deblock=0," + db;\r
454                             }\r
455                             else if ((!da.Contains("Default")) && (!db.Contains("Default")))\r
456                                 thisOpt = "deblock=" + da + "," + db;\r
457                         }\r
458                         else if (optNameToChange.Equals("aq-strength"))\r
459                         {\r
460                             if (slider_adaptiveQuantStrength.Value == 10)\r
461                                 thisOpt = string.Empty;\r
462                             else\r
463                             {\r
464                                 double value = slider_adaptiveQuantStrength.Value * 0.1;\r
465                                 string aqs = value.ToString("f1");\r
466                                 thisOpt = "aq-strength=" + aqs;\r
467                             }\r
468                         }\r
469                         else if (optNameToChange.Equals("psy-rd"))\r
470                         {\r
471                             if (slider_psyrd.Value == 10 && slider_psytrellis.Value == 0)\r
472                                 thisOpt = string.Empty;\r
473                             else\r
474                             {\r
475                                 double psyrd = slider_psyrd.Value * 0.1;\r
476                                 double psytre = slider_psytrellis.Value * 0.05;\r
477 \r
478                                 string rd = psyrd.ToString("f2");\r
479                                 string rt = psytre.ToString("f2");\r
480 \r
481                                 thisOpt = "psy-rd=" + rd + "," + rt;\r
482                             }\r
483                         }\r
484                         else if (optNameToChange.Equals("b-pyramid"))\r
485                         {\r
486                             switch (combo_pyrmidalBFrames.SelectedIndex)\r
487                             {\r
488                                 case 0: // Default\r
489                                     thisOpt = string.Empty;\r
490                                     break;\r
491                                 case 1: // Off\r
492                                     thisOpt = "b-pyramid=none";\r
493                                     break;\r
494                                 case 2: // Strict\r
495                                     thisOpt = "b-pyramid=strict";\r
496                                     break;\r
497                             }\r
498                         }\r
499                         else if (optNameToChange.Equals("no-dct-decimate"))\r
500                             thisOpt = check_noDCTDecimate.CheckState == CheckState.Checked ? "no-dct-decimate=1" : string.Empty;\r
501                         else if (optNameToChange.Equals("8x8dct"))\r
502                             thisOpt = check_8x8DCT.CheckState == CheckState.Unchecked ? "8x8dct=0" : string.Empty;\r
503                         else if (optNameToChange.Equals("cabac"))\r
504                             thisOpt = check_Cabac.CheckState == CheckState.Checked ? string.Empty : "cabac=0";\r
505                         else if (optNameToChange.Equals("weightp"))\r
506                             thisOpt = check_weightp.CheckState == CheckState.Checked ? string.Empty : "weightp=0";\r
507                         else if (optNameToChange.Equals("me"))\r
508                         {\r
509                             switch (drop_MotionEstimationMethod.SelectedIndex)\r
510                             {\r
511                                 case 1:\r
512                                     thisOpt = "me=dia";\r
513                                     break;\r
514 \r
515                                 case 2:\r
516                                     thisOpt = "me=hex";\r
517                                     break;\r
518 \r
519                                 case 3:\r
520                                     thisOpt = "me=umh";\r
521                                     break;\r
522 \r
523                                 case 4:\r
524                                     thisOpt = "me=esa";\r
525                                     break;\r
526 \r
527                                 case 5:\r
528                                     thisOpt = "me=tesa";\r
529                                     break;\r
530 \r
531                                 default:\r
532                                     thisOpt = string.Empty;\r
533                                     break;\r
534                             }\r
535                         }\r
536                         else if (optNameToChange.Equals("direct"))\r
537                         {\r
538                             switch (drop_directPrediction.SelectedIndex)\r
539                             {\r
540                                 case 1:\r
541                                     thisOpt = "direct=none";\r
542                                     break;\r
543 \r
544                                 case 2:\r
545                                     thisOpt = "direct=spatial";\r
546                                     break;\r
547 \r
548                                 case 3:\r
549                                     thisOpt = "direct=temporal";\r
550                                     break;\r
551 \r
552                                 case 4:\r
553                                     thisOpt = "direct=auto";\r
554                                     break;\r
555 \r
556                                 default:\r
557                                     thisOpt = string.Empty;\r
558                                     break;\r
559                             }\r
560                         }\r
561                         else if (optNameToChange.Equals("analyse"))\r
562                         {\r
563                             switch (drop_analysis.SelectedIndex)\r
564                             {\r
565                                 case 1:\r
566                                     thisOpt = "analyse=none";\r
567                                     break;\r
568 \r
569                                 case 2:\r
570                                     thisOpt = "analyse=some";\r
571                                     break;\r
572 \r
573                                 case 3:\r
574                                     thisOpt = "analyse=all";\r
575                                     break;\r
576 \r
577                                 default:\r
578                                     thisOpt = string.Empty;\r
579                                     break;\r
580                             }\r
581                         }\r
582                         else if (optNameToChange.Equals("merange"))\r
583                         {\r
584                             thisOpt = !drop_MotionEstimationRange.SelectedItem.ToString().Contains("Default")\r
585                                           ? "merange=" + drop_MotionEstimationRange.SelectedItem\r
586                                           : string.Empty;\r
587                         }\r
588                         else if (optNameToChange.Equals("b-adapt"))\r
589                         {\r
590                             thisOpt = !drop_adaptBFrames.SelectedItem.ToString().Contains("Default")\r
591                                           ? "b-adapt=" + (drop_adaptBFrames.SelectedIndex - 1)\r
592                                           : string.Empty;\r
593                         }\r
594                         else if (optNameToChange.Equals("ref"))\r
595                         {\r
596                             thisOpt = !drop_refFrames.SelectedItem.ToString().Contains("Default")\r
597                                           ? "ref=" + drop_refFrames.SelectedItem\r
598                                           : string.Empty;\r
599                         }\r
600                         else if (optNameToChange.Equals("bframes"))\r
601                         {\r
602                             string value = drop_bFrames.SelectedItem.ToString();\r
603                             thisOpt = !drop_bFrames.SelectedItem.ToString().Contains("Default")\r
604                                           ? "bframes=" + value\r
605                                           : string.Empty;\r
606                         }\r
607                         else if (optNameToChange.Equals("subq"))\r
608                         {\r
609                             string value = drop_subpixelMotionEstimation.SelectedItem.ToString();\r
610                             string[] val = value.Split(':');\r
611                             thisOpt = !drop_subpixelMotionEstimation.SelectedItem.ToString().Contains("Default")\r
612                                           ? "subq=" + val[0]\r
613                                           : string.Empty;\r
614                         }\r
615                         else if (optNameToChange.Equals("trellis"))\r
616                         {\r
617                             switch (drop_trellis.SelectedIndex)\r
618                             {\r
619                                 case 1: // Off\r
620                                     thisOpt = "trellis=0";\r
621                                     break;\r
622                                 case 2: // Encode Only\r
623                                     thisOpt = "trellis=1";\r
624                                     break;\r
625                                 case 3: // Always\r
626                                     thisOpt = "trellis=2";\r
627                                     break;\r
628                                 default:\r
629                                     thisOpt = string.Empty;\r
630                                     break;\r
631                             }\r
632                         }\r
633                     }\r
634                 }\r
635 \r
636                 /* Construct New String for opts here */\r
637                 if (!thisOpt.Equals(string.Empty))\r
638                     changedOptString = changedOptString.Equals(string.Empty) ? thisOpt : changedOptString + ":" + thisOpt;\r
639             }\r
640 \r
641             /* Change the option string to reflect the new mod settings */\r
642             rtf_x264Query.Text = changedOptString;\r
643         }\r
644 \r
645         /// <summary>\r
646         /// Add's an option to the x264 query string.\r
647         /// Handles 2 cases.  1 Where rtf_x264Query.Text is empty, and one where there is an option with no value,\r
648         /// e.g no-fast-pskip\r
649         /// </summary>\r
650         /// <param name="optNameToChange">The Option Name to Change</param>\r
651         private void HasNoOptions(IEquatable<string> optNameToChange)\r
652         {\r
653             string colon = string.Empty;\r
654             if (rtf_x264Query.Text != string.Empty)\r
655                 colon = ":";\r
656 \r
657             string query = rtf_x264Query.Text;\r
658             if (optNameToChange.Equals("me"))\r
659             {\r
660                 switch (drop_MotionEstimationMethod.SelectedIndex)\r
661                 {\r
662                     case 1:\r
663                         query = query + colon + "me=dia";\r
664                         break;\r
665 \r
666                     case 2:\r
667                         query = query + colon + "me=hex";\r
668                         break;\r
669 \r
670                     case 3:\r
671                         query = query + colon + "me=umh";\r
672                         break;\r
673 \r
674                     case 4:\r
675                         query = query + colon + "me=esa";\r
676                         break;\r
677 \r
678                     case 5:\r
679                         query = query + colon + "me=tesa";\r
680                         break;\r
681 \r
682                     default:\r
683                         break;\r
684                 }\r
685             }\r
686             else if (optNameToChange.Equals("direct"))\r
687             {\r
688                 switch (drop_directPrediction.SelectedIndex)\r
689                 {\r
690                     case 1:\r
691                         query = query + colon + "direct=none";\r
692                         break;\r
693 \r
694                     case 2:\r
695                         query = query + colon + "direct=spatial";\r
696                         break;\r
697 \r
698                     case 3:\r
699                         query = query + colon + "direct=temporal";\r
700                         break;\r
701 \r
702                     case 4:\r
703                         query = query + colon + "direct=auto";\r
704                         break;\r
705 \r
706                     default:\r
707                         break;\r
708                 }\r
709             }\r
710             else if (optNameToChange.Equals("analyse"))\r
711             {\r
712                 switch (drop_analysis.SelectedIndex)\r
713                 {\r
714                     case 1:\r
715                         query = query + colon + "analyse=none";\r
716                         break;\r
717 \r
718                     case 2:\r
719                         query = query + colon + "analyse=some";\r
720                         break;\r
721 \r
722                     case 3:\r
723                         query = query + colon + "analyse=all";\r
724                         break;\r
725 \r
726                     default:\r
727                         break;\r
728                 }\r
729             }\r
730             else if (optNameToChange.Equals("merange"))\r
731             {\r
732                 int value = drop_MotionEstimationRange.SelectedIndex + 3;\r
733                 query = query + colon + "merange=" + value;\r
734             }\r
735             else if (optNameToChange.Equals("b-adapt"))\r
736             {\r
737                 int value = drop_adaptBFrames.SelectedIndex - 1;\r
738                 query = query + colon + "b-adapt=" + value;\r
739             }\r
740             else if (optNameToChange.Equals("deblock"))\r
741             {\r
742                 string da = drop_deblockAlpha.SelectedItem.ToString();\r
743                 string db = drop_deblockBeta.Text;\r
744 \r
745                 if (((da.Contains("Default")) && (db.Contains("Default"))) || ((da.Contains("0")) && (db.Contains("0"))))\r
746                 {\r
747                     drop_deblockBeta.SelectedItem = "Default (0)";\r
748                     drop_deblockAlpha.SelectedItem = "Default (0)";\r
749                 }\r
750                 else\r
751                 {\r
752                     if (db.Contains("Default"))\r
753                         db = "0";\r
754 \r
755                     if (da.Contains("Default"))\r
756                         da = "0";\r
757 \r
758                     query = query + colon + "deblock=" + da + "," + db;\r
759                 }\r
760             }\r
761             else if (optNameToChange.Equals("aq-strength"))\r
762             {\r
763                 if (slider_adaptiveQuantStrength.Value == 10)\r
764                     query = string.Empty;\r
765                 else\r
766                 {\r
767                     double value = slider_adaptiveQuantStrength.Value * 0.1;\r
768                     string aqs = value.ToString("f1");\r
769                     query += colon + "aq-strength=" + aqs;\r
770                 }\r
771             }\r
772             else if (optNameToChange.Equals("psy-rd"))\r
773             {\r
774                 if (slider_psyrd.Value == 10 && slider_psytrellis.Value == 0)\r
775                     query += string.Empty;\r
776                 else\r
777                 {\r
778                     double psyrd = slider_psyrd.Value * 0.1;\r
779                     double psytre = slider_psytrellis.Value * 0.05;\r
780 \r
781                     string rd = psyrd.ToString("f1");\r
782                     string rt = psytre.ToString("f2");\r
783 \r
784                     query += colon + "psy-rd=" + rd + "," + rt;\r
785                 }\r
786             }\r
787             else if (optNameToChange.Equals("b-pyramid"))\r
788             {\r
789                 switch (combo_pyrmidalBFrames.SelectedIndex)\r
790                 {\r
791                     case 0:\r
792                         break;\r
793                     case 1:\r
794                         query = query + colon + "b-pyramid=none";\r
795                         break;\r
796                     case 2:\r
797                         query = query + colon + "b-pyramid=strict";\r
798                         break;\r
799                 }\r
800             }\r
801             else if (optNameToChange.Equals("no-dct-decimate"))\r
802             {\r
803                 if (check_noDCTDecimate.CheckState == CheckState.Checked)\r
804                     query = query + colon + "no-dct-decimate=1";\r
805             }\r
806             else if (optNameToChange.Equals("8x8dct"))\r
807             {\r
808                 if (check_8x8DCT.CheckState == CheckState.Unchecked)\r
809                     query = query + colon + "8x8dct=0";\r
810             }\r
811             else if (optNameToChange.Equals("cabac"))\r
812             {\r
813                 if (check_Cabac.CheckState != CheckState.Checked)\r
814                     query = query + colon + "cabac=0";\r
815             }\r
816             else if (optNameToChange.Equals("weightp"))\r
817             {\r
818                 if (check_weightp.CheckState == CheckState.Unchecked)\r
819                     query = query + colon + "weightp=0";\r
820             }\r
821             else if (optNameToChange.Equals("ref"))\r
822             {\r
823                 if (!drop_refFrames.SelectedItem.ToString().Contains("Default"))\r
824                     query = query + colon + "ref=" + drop_refFrames.SelectedItem;\r
825             }\r
826             else if (optNameToChange.Equals("bframes"))\r
827             {\r
828                 string value = drop_bFrames.SelectedItem.ToString();\r
829                 if (!drop_bFrames.SelectedItem.ToString().Contains("Default"))\r
830                     query = query + colon + "bframes=" + value;\r
831             }\r
832             else if (optNameToChange.Equals("subq"))\r
833             {\r
834                 string value = drop_subpixelMotionEstimation.SelectedItem.ToString();\r
835                 if (!drop_subpixelMotionEstimation.SelectedItem.ToString().Contains("Default"))\r
836                 {\r
837                     string[] val = value.Split(':');\r
838                     query = query + colon + "subq=" + val[0];\r
839                 }\r
840             }\r
841             else if (optNameToChange.Equals("trellis"))\r
842             {\r
843                 switch (drop_trellis.SelectedIndex)\r
844                 {\r
845                     case 1: // Off\r
846                         query = query + colon + "trellis=0";\r
847                         break;\r
848                     case 2: // Encode Only\r
849                         query = query + colon + "trellis=1";\r
850                         break;\r
851                     case 3: // Always\r
852                         query = query + colon + "trellis=2";\r
853                         break;\r
854                     default:\r
855                         break;\r
856                 }\r
857             }\r
858 \r
859             rtf_x264Query.Text = query;\r
860         }\r
861 \r
862         /// <summary>\r
863         /// Shows and hides controls based on the values of other controls.\r
864         /// </summary>\r
865         /// <param name="sender">The Sender</param>\r
866         private void Animate(string sender)\r
867         {\r
868             /* Lots of situations to cover.\r
869                - B-frames (when 0 turn of b-frame specific stuff, when < 2 disable b-pyramid)\r
870                - CABAC (when 0 turn off trellis and psy-trel\r
871                - subme (if under 6, turn off psy-rd and psy-trel)\r
872                - trellis (if 0, turn off psy-trel)\r
873              */\r
874 \r
875             switch (sender)\r
876             {\r
877                 case "bframes":\r
878                     if (drop_bFrames.SelectedIndex == 1)\r
879                     {\r
880                         /* If the b-frame widget is at 1, the user has chosen\r
881                            not to use b-frames at all. So disable the options\r
882                            that can only be used when b-frames are enabled.        */\r
883                         combo_pyrmidalBFrames.Visible = false;\r
884                         lbl_prymidalBframes.Visible = false;\r
885                         drop_directPrediction.Visible = false;\r
886                         lbl_direct_prediction.Visible = false;\r
887 \r
888                         combo_pyrmidalBFrames.SelectedIndex = 0;\r
889                         drop_directPrediction.SelectedIndex = 0;\r
890 \r
891                         drop_adaptBFrames.Visible = false;\r
892                         lbl_adaptBFrames.Visible = false;\r
893                         drop_adaptBFrames.SelectedIndex = 0;\r
894                     }\r
895                     else if (drop_bFrames.SelectedIndex == 2)\r
896                     {\r
897                         /* Only 1 b-frame? Disable b-pyramid. */\r
898                         combo_pyrmidalBFrames.Visible = false;\r
899                         lbl_prymidalBframes.Visible = false;\r
900                         combo_pyrmidalBFrames.SelectedIndex = 0;\r
901 \r
902                         drop_directPrediction.Visible = true;\r
903                         lbl_direct_prediction.Visible = true;\r
904 \r
905                         drop_adaptBFrames.Visible = true;\r
906                         lbl_adaptBFrames.Visible = true;\r
907                     }\r
908                     else\r
909                     {\r
910                         combo_pyrmidalBFrames.Visible = true;\r
911                         lbl_prymidalBframes.Visible = true;\r
912                         drop_directPrediction.Visible = true;\r
913                         lbl_direct_prediction.Visible = true;\r
914 \r
915                         drop_adaptBFrames.Visible = true;\r
916                         lbl_adaptBFrames.Visible = true;\r
917                     }\r
918                     break;\r
919                 case "cabac":\r
920                     if (check_Cabac.Checked == false)\r
921                     {\r
922                         /* Without CABAC entropy coding, trellis doesn't run. */\r
923                         drop_trellis.Visible = false;\r
924                         drop_trellis.SelectedIndex = 0;\r
925                         lbl_trellis.Visible = false;\r
926 \r
927                         slider_psytrellis.Visible = false;\r
928                         lbl_psytrellis.Visible = false;\r
929                         slider_psytrellis.Value = 0;\r
930                     }\r
931                     else\r
932                     {\r
933                         drop_trellis.Visible = true;\r
934                         lbl_trellis.Visible = true;\r
935 \r
936                         slider_psytrellis.Visible = true;\r
937                         lbl_psytrellis.Visible = true;\r
938                     }\r
939                     break;\r
940                 case "me": // Motion Estimation\r
941                     if (drop_MotionEstimationMethod.SelectedIndex < 3)\r
942                     {\r
943                         drop_MotionEstimationRange.Visible = false;\r
944                         lbl_merange.Visible = false;\r
945                         drop_MotionEstimationRange.SelectedIndex = 0;\r
946                     }\r
947                     else\r
948                     {\r
949                         drop_MotionEstimationRange.Visible = true;\r
950                         lbl_merange.Visible = true;\r
951                     }\r
952                     break;\r
953                 case "subq": // subme\r
954                     if (drop_subpixelMotionEstimation.SelectedIndex != 0 &&\r
955                         drop_subpixelMotionEstimation.SelectedIndex < 7)\r
956                     {\r
957                         slider_psyrd.Visible = false;\r
958                         slider_psyrd.Value = 10;\r
959                         lbl_psyrd.Visible = false;\r
960 \r
961                         slider_psytrellis.Visible = false;\r
962                         slider_psytrellis.Value = 0;\r
963                         lbl_psytrellis.Visible = false;\r
964                     }\r
965                     else\r
966                     {\r
967                         slider_psyrd.Visible = true;\r
968                         lbl_psyrd.Visible = true;\r
969 \r
970                         if (drop_trellis.SelectedIndex >= 2 && check_Cabac.Checked && slider_psytrellis.Visible == false)\r
971                         {\r
972                             slider_psytrellis.Visible = true;\r
973                             lbl_psytrellis.Visible = true;\r
974                         }\r
975                     }\r
976                     break;\r
977                 case "trellis": // subme\r
978                     if (drop_trellis.SelectedIndex > 0 && drop_trellis.SelectedIndex < 2)\r
979                     {\r
980                         slider_psytrellis.Visible = false;\r
981                         slider_psytrellis.Value = 0;\r
982                         lbl_psytrellis.Visible = false;\r
983                     }\r
984                     else\r
985                     {\r
986                         if ((drop_subpixelMotionEstimation.SelectedIndex == 0 ||\r
987                              drop_subpixelMotionEstimation.SelectedIndex >= 7) && check_Cabac.Checked &&\r
988                             slider_psytrellis.Visible == false)\r
989                         {\r
990                             slider_psytrellis.Visible = true;\r
991                             lbl_psytrellis.Visible = true;\r
992                         }\r
993                     }\r
994                     break;\r
995             }\r
996         }\r
997 \r
998         /* UI Events */\r
999 \r
1000         private void widgetControlChanged(object sender, EventArgs e)\r
1001         {\r
1002             Control changedControlName = (Control) sender;\r
1003             string controlName = string.Empty;\r
1004 \r
1005             switch (changedControlName.Name.Trim())\r
1006             {\r
1007                 case "drop_refFrames":\r
1008                     controlName = "ref";\r
1009                     break;\r
1010                 case "drop_bFrames":\r
1011                     controlName = "bframes";\r
1012                     break;\r
1013                 case "drop_directPrediction":\r
1014                     controlName = "direct";\r
1015                     break;\r
1016                 case "check_weightp":\r
1017                     controlName = "weightp";\r
1018                     break;\r
1019                 case "combo_pyrmidalBFrames":\r
1020                     controlName = "b-pyramid";\r
1021                     break;\r
1022                 case "drop_MotionEstimationMethod":\r
1023                     controlName = "me";\r
1024                     break;\r
1025                 case "drop_MotionEstimationRange":\r
1026                     controlName = "merange";\r
1027                     break;\r
1028                 case "drop_subpixelMotionEstimation":\r
1029                     controlName = "subq";\r
1030                     break;\r
1031                 case "drop_analysis":\r
1032                     controlName = "analyse";\r
1033                     break;\r
1034                 case "check_8x8DCT":\r
1035                     controlName = "8x8dct";\r
1036                     break;\r
1037                 case "drop_deblockAlpha":\r
1038                     controlName = "deblock";\r
1039                     break;\r
1040                 case "drop_deblockBeta":\r
1041                     controlName = "deblock";\r
1042                     break;\r
1043                 case "drop_trellis":\r
1044                     controlName = "trellis";\r
1045                     break;\r
1046                 case "check_noDCTDecimate":\r
1047                     controlName = "no-dct-decimate";\r
1048                     break;\r
1049                 case "check_Cabac":\r
1050                     controlName = "cabac";\r
1051                     break;\r
1052                 case "slider_psyrd":\r
1053                     controlName = "psy-rd";\r
1054                     break;\r
1055                 case "slider_psytrellis":\r
1056                     controlName = "psy-rd";\r
1057                     break;\r
1058                 case "slider_adaptiveQuantStrength":\r
1059                     controlName = "aq-strength";\r
1060                     break;\r
1061                 case "drop_adaptBFrames":\r
1062                     controlName = "b-adapt";\r
1063                     break;\r
1064             }\r
1065             OnX264WidgetChange(controlName);\r
1066         }\r
1067 \r
1068         private void rtf_x264Query_TextChanged(object sender, EventArgs e)\r
1069         {\r
1070             if (rtf_x264Query.Text.EndsWith("\n"))\r
1071             {\r
1072                 string query = rtf_x264Query.Text.Replace("\n", string.Empty);\r
1073                 Reset2Defaults();\r
1074                 rtf_x264Query.Text = query;\r
1075                 this.StandardizeOptString();\r
1076                 this.SetCurrentSettingsInPanel();\r
1077 \r
1078                 if (rtf_x264Query.Text == string.Empty)\r
1079                     Reset2Defaults();\r
1080             }\r
1081         }\r
1082 \r
1083         private void btn_reset_Click(object sender, EventArgs e)\r
1084         {\r
1085             rtf_x264Query.Text = string.Empty;\r
1086             Reset2Defaults();\r
1087         }\r
1088     }\r
1089 }