OSDN Git Service

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