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.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                                 drop_subpixelMotionEstimation.SelectedItem = optValue;\r
241                                 continue;\r
242                             case "trellis":\r
243                                 switch (optValue)\r
244                                 {\r
245                                     case "0":\r
246                                         drop_trellis.SelectedIndex = 1;\r
247                                         break;\r
248                                     case "1":\r
249                                          drop_trellis.SelectedIndex = 2;\r
250                                         break;\r
251                                     case "2":\r
252                                         drop_trellis.SelectedIndex = 3;\r
253                                         break;\r
254                                 }\r
255                                 continue;\r
256                             case "me":\r
257                                 if (optValue.Equals("dia"))\r
258                                     drop_MotionEstimationMethod.SelectedItem = "Diamond";\r
259                                 else if (optValue.Equals("hex"))\r
260                                     drop_MotionEstimationMethod.SelectedItem = "Hexagon";\r
261                                 else if (optValue.Equals("umh"))\r
262                                     drop_MotionEstimationMethod.SelectedItem = "Uneven Multi-Hexagon";\r
263                                 else if (optValue.Equals("esa"))\r
264                                     drop_MotionEstimationMethod.SelectedItem = "Exhaustive";\r
265                                 else if (optValue.Equals("tesa"))\r
266                                     drop_MotionEstimationMethod.SelectedItem = "Transformed Exhaustive";\r
267                                 continue;\r
268                             case "merange":\r
269                                 drop_MotionEstimationRange.SelectedItem = optValue;\r
270                                 continue;\r
271                             case "b-adapt":\r
272                                 int badapt;\r
273                                 int.TryParse(optValue, out badapt);\r
274                                 drop_adaptBFrames.SelectedIndex = (badapt + 1);\r
275                                 continue;\r
276                             case "b-pyramid":\r
277                                 switch (optValue)\r
278                                 {\r
279                                     case "normal":\r
280                                         combo_pyrmidalBFrames.SelectedIndex = 0;\r
281                                         break;\r
282                                     case "strict":\r
283                                         combo_pyrmidalBFrames.SelectedIndex = 2;\r
284                                         break;\r
285                                     case "none":\r
286                                         combo_pyrmidalBFrames.SelectedIndex = 1;\r
287                                         break;\r
288                                 }\r
289                                 continue;\r
290                             case "direct":\r
291                                 if (optValue == "auto")\r
292                                     optValue = "Automatic";\r
293 \r
294                                 if (optValue != string.Empty)\r
295                                 {\r
296                                     char[] letters = optValue.ToCharArray();\r
297                                     letters[0] = Char.ToUpper(letters[0]);\r
298                                     optValue = new string(letters);\r
299                                 }\r
300 \r
301                                 drop_directPrediction.SelectedItem = optValue;\r
302                                 continue;\r
303                             case "deblock":\r
304                                 string[] splitDeblock = optValue.Split(',');\r
305                                 string alphaDeblock = splitDeblock[0];\r
306                                 string betaDeblock = splitDeblock[1];\r
307 \r
308                                 if (alphaDeblock.Equals("0") && betaDeblock.Replace("\n", string.Empty).Equals("0"))\r
309                                 {\r
310                                     drop_deblockAlpha.SelectedItem = "Default (0)";\r
311                                     drop_deblockBeta.SelectedItem = "Default (0)";\r
312                                 }\r
313                                 else\r
314                                 {\r
315                                     drop_deblockAlpha.SelectedItem = !alphaDeblock.Equals("0") ? alphaDeblock : "0";\r
316 \r
317                                     drop_deblockBeta.SelectedItem = !betaDeblock.Replace("\n", string.Empty).Equals("0")\r
318                                                                         ? betaDeblock.Replace("\n", string.Empty)\r
319                                                                         : "0";\r
320                                 }\r
321                                 continue;\r
322                             case "analyse":\r
323                                 if (optValue.Equals("p8x8,b8x8,i8x8,i4x4"))\r
324                                     drop_analysis.SelectedItem = "Default (most)";\r
325                                 if (optValue.Equals("none"))\r
326                                     drop_analysis.SelectedItem = "None";\r
327                                 if (optValue.Equals("i4x4,i8x8"))\r
328                                     drop_analysis.SelectedItem = "Some";\r
329                                 if (optValue.Equals("all"))\r
330                                     drop_analysis.SelectedItem = "All";\r
331                                 continue;\r
332                             case "8x8dct":\r
333                                 check_8x8DCT.CheckState = optValue == "1" ? CheckState.Checked : CheckState.Unchecked;\r
334                                 continue;\r
335                             case "aq-strength":\r
336                                 float value;\r
337                                 float.TryParse(optValue, out value);\r
338                                 int sliderValue;\r
339                                 int.TryParse((value * 10).ToString(), out sliderValue);\r
340                                 slider_adaptiveQuantStrength.Value = sliderValue;\r
341                                 continue;\r
342                             case "cabac":\r
343                                 check_Cabac.CheckState = CheckState.Unchecked;\r
344                                 continue;\r
345                             case "psy-rd":\r
346                                 string[] x = optValue.Split(',');\r
347 \r
348                                 double psyrd, psytrellis;\r
349                                 int val, val2;\r
350 \r
351                                 // default psy-rd = 1 (10 for the slider)\r
352                                 psyrd = double.TryParse(x[0], out psyrd) ? psyrd * 10 : 10.0;\r
353                                 // default psy-trellis = 0\r
354                                 psytrellis = double.TryParse(x[1], out psytrellis) ? psytrellis * 20 : 0.0;\r
355 \r
356                                 int.TryParse(psyrd.ToString(), out val);\r
357                                 int.TryParse(psytrellis.ToString(), out val2);\r
358 \r
359                                 slider_psyrd.Value = val;\r
360                                 slider_psytrellis.Value = val2;\r
361                                 continue;\r
362                         }\r
363                     }\r
364                 }\r
365             }\r
366         }\r
367 \r
368         /// <summary>\r
369         /// This function will update the X264 Query when one of the GUI widgets changes.\r
370         /// </summary>\r
371         /// <param name="sender">\r
372         /// The sender.\r
373         /// </param>\r
374         private void OnX264WidgetChange(string sender)\r
375         {\r
376             Animate(sender);\r
377             string optNameToChange = sender;\r
378             string currentOptString = rtf_x264Query.Text;\r
379 \r
380             /*First, we create a pattern to check for ":"optNameToChange"=" to modify the option if the name falls after\r
381                 the first character of the opt string (hence the ":") */\r
382             string checkOptNameToChange = ":" + optNameToChange + "=";\r
383             string checkOptNameToChangeBegin = optNameToChange + "=";\r
384 \r
385             // IF the current H264 Option String Contains Multiple Items or Just 1 Item\r
386             if ((currentOptString.Contains(checkOptNameToChange)) || (currentOptString.StartsWith(checkOptNameToChangeBegin)))\r
387                 HasOptions(currentOptString, optNameToChange);\r
388             else // IF there is no options in the rich text box!\r
389                 HasNoOptions(optNameToChange);\r
390         }\r
391 \r
392         /// <summary>\r
393         /// Called when the current x264 option string contains multiple (or a single) item(s) in it seperated by :\r
394         /// it updates the current option that the widget corrosponds to, if it is already in thes string.\r
395         /// </summary>\r
396         /// <param name="currentOptString">The Current Option String</param>\r
397         /// <param name="optNameToChange">Name of the option to change</param>\r
398         private void HasOptions(string currentOptString, string optNameToChange)\r
399         {\r
400             string thisOpt; // The separated option such as "bframes=3"\r
401             string optName; // The option name such as "bframes"\r
402             string[] currentOptsArray;\r
403 \r
404             /* Create new empty opt string*/\r
405             string changedOptString = string.Empty;\r
406 \r
407             /*Put individual options into an array based on the ":" separator for processing, result is "<opt>=<value>"*/\r
408             currentOptsArray = currentOptString.Split(':');\r
409 \r
410             /*iterate through the array and get <opts> and <values*/\r
411             for (int loopcounter = 0; loopcounter < currentOptsArray.Length; loopcounter++)\r
412             {\r
413                 thisOpt = currentOptsArray[loopcounter];\r
414 \r
415                 if (thisOpt.Contains("="))\r
416                 {\r
417                     string[] splitOptRange = thisOpt.Split('=');\r
418 \r
419                     optName = splitOptRange[0]; // e.g bframes\r
420 \r
421                     /* \r
422                      * Run through the available widgets for x264 opts and set them, as you add widgets,\r
423                      * they need to be added here. This should be moved to its own method probably\r
424                      * If the optNameToChange is found, appropriately change the value or delete it if\r
425                      * "unspecified" is set.\r
426                      */\r
427                     if (optName.Equals(optNameToChange))\r
428                     {\r
429                         if (optNameToChange.Equals("deblock"))\r
430                         {\r
431                             string da = drop_deblockAlpha.SelectedItem.ToString();\r
432                             string db = drop_deblockBeta.SelectedItem.ToString();\r
433 \r
434                             if (((da.Contains("Default")) && (db.Contains("Default"))) ||\r
435                                 ((da.Contains("0")) && (db.Contains("0"))))\r
436                             {\r
437                                 drop_deblockBeta.SelectedItem = "Default (0)";\r
438                                 drop_deblockAlpha.SelectedItem = "Default (0)";\r
439                                 thisOpt = string.Empty;\r
440                             }\r
441                             else if ((!da.Contains("Default")) && (db.Contains("Default")))\r
442                             {\r
443                                 drop_deblockBeta.SelectedItem = "0";\r
444                                 thisOpt = "deblock=" + da + ",0";\r
445                             }\r
446                             else if ((da.Contains("Default")) && (!db.Contains("Default")))\r
447                             {\r
448                                 drop_deblockAlpha.SelectedItem = "0";\r
449                                 thisOpt = "deblock=0," + db;\r
450                             }\r
451                             else if ((!da.Contains("Default")) && (!db.Contains("Default")))\r
452                                 thisOpt = "deblock=" + da + "," + db;\r
453                         }\r
454                         else if (optNameToChange.Equals("aq-strength"))\r
455                         {\r
456                             if (slider_adaptiveQuantStrength.Value == 10)\r
457                                 thisOpt = string.Empty;\r
458                             else\r
459                             {\r
460                                 double value = slider_adaptiveQuantStrength.Value * 0.1;\r
461                                 string aqs = value.ToString("f1");\r
462                                 thisOpt = "aq-strength=" + aqs;\r
463                             }\r
464                         }\r
465                         else if (optNameToChange.Equals("psy-rd"))\r
466                         {\r
467                             if (slider_psyrd.Value == 10 && slider_psytrellis.Value == 0)\r
468                                 thisOpt = string.Empty;\r
469                             else\r
470                             {\r
471                                 double psyrd = slider_psyrd.Value * 0.1;\r
472                                 double psytre = slider_psytrellis.Value * 0.05;\r
473 \r
474                                 string rd = psyrd.ToString("f2");\r
475                                 string rt = psytre.ToString("f2");\r
476 \r
477                                 thisOpt = "psy-rd=" + rd + "," + rt;\r
478                             }\r
479                         }\r
480                         else if (optNameToChange.Equals("b-pyramid"))\r
481                         {\r
482                             switch (combo_pyrmidalBFrames.SelectedIndex)\r
483                             {\r
484                                 case 0: // Default\r
485                                     thisOpt = string.Empty;\r
486                                     break;\r
487                                 case 1: // Off\r
488                                     thisOpt = "b-pyramid=none";\r
489                                     break;\r
490                                 case 2: // Strict\r
491                                     thisOpt = "b-pyramid=strict";\r
492                                     break;\r
493                             }\r
494                         }\r
495                         else if (optNameToChange.Equals("no-dct-decimate"))\r
496                             thisOpt = check_noDCTDecimate.CheckState == CheckState.Checked ? "no-dct-decimate=1" : string.Empty;\r
497                         else if (optNameToChange.Equals("8x8dct"))\r
498                             thisOpt = check_8x8DCT.CheckState == CheckState.Unchecked ? "8x8dct=0" : string.Empty;\r
499                         else if (optNameToChange.Equals("cabac"))\r
500                             thisOpt = check_Cabac.CheckState == CheckState.Checked ? string.Empty : "cabac=0";\r
501                         else if (optNameToChange.Equals("weightp"))\r
502                             thisOpt = check_weightp.CheckState == CheckState.Checked ? string.Empty : "weightp=0";\r
503                         else if (optNameToChange.Equals("me"))\r
504                         {\r
505                             switch (drop_MotionEstimationMethod.SelectedIndex)\r
506                             {\r
507                                 case 1:\r
508                                     thisOpt = "me=dia";\r
509                                     break;\r
510 \r
511                                 case 2:\r
512                                     thisOpt = "me=hex";\r
513                                     break;\r
514 \r
515                                 case 3:\r
516                                     thisOpt = "me=umh";\r
517                                     break;\r
518 \r
519                                 case 4:\r
520                                     thisOpt = "me=esa";\r
521                                     break;\r
522 \r
523                                 case 5:\r
524                                     thisOpt = "me=tesa";\r
525                                     break;\r
526 \r
527                                 default:\r
528                                     thisOpt = string.Empty;\r
529                                     break;\r
530                             }\r
531                         }\r
532                         else if (optNameToChange.Equals("direct"))\r
533                         {\r
534                             switch (drop_directPrediction.SelectedIndex)\r
535                             {\r
536                                 case 1:\r
537                                     thisOpt = "direct=none";\r
538                                     break;\r
539 \r
540                                 case 2:\r
541                                     thisOpt = "direct=spatial";\r
542                                     break;\r
543 \r
544                                 case 3:\r
545                                     thisOpt = "direct=temporal";\r
546                                     break;\r
547 \r
548                                 case 4:\r
549                                     thisOpt = "direct=auto";\r
550                                     break;\r
551 \r
552                                 default:\r
553                                     thisOpt = string.Empty;\r
554                                     break;\r
555                             }\r
556                         }\r
557                         else if (optNameToChange.Equals("analyse"))\r
558                         {\r
559                             switch (drop_analysis.SelectedIndex)\r
560                             {\r
561                                 case 1:\r
562                                     thisOpt = "analyse=none";\r
563                                     break;\r
564 \r
565                                 case 2:\r
566                                     thisOpt = "analyse=some";\r
567                                     break;\r
568 \r
569                                 case 3:\r
570                                     thisOpt = "analyse=all";\r
571                                     break;\r
572 \r
573                                 default:\r
574                                     thisOpt = string.Empty;\r
575                                     break;\r
576                             }\r
577                         }\r
578                         else if (optNameToChange.Equals("merange"))\r
579                         {\r
580                             thisOpt = !drop_MotionEstimationRange.SelectedItem.ToString().Contains("Default")\r
581                                           ? "merange=" + drop_MotionEstimationRange.SelectedItem\r
582                                           : string.Empty;\r
583                         }\r
584                         else if (optNameToChange.Equals("b-adapt"))\r
585                         {\r
586                             thisOpt = !drop_adaptBFrames.SelectedItem.ToString().Contains("Default")\r
587                                           ? "b-adapt=" + (drop_adaptBFrames.SelectedIndex - 1)\r
588                                           : string.Empty;\r
589                         }\r
590                         else if (optNameToChange.Equals("ref"))\r
591                         {\r
592                             thisOpt = !drop_refFrames.SelectedItem.ToString().Contains("Default")\r
593                                           ? "ref=" + drop_refFrames.SelectedItem\r
594                                           : string.Empty;\r
595                         }\r
596                         else if (optNameToChange.Equals("bframes"))\r
597                         {\r
598                             string value = drop_bFrames.SelectedItem.ToString();\r
599                             thisOpt = !drop_bFrames.SelectedItem.ToString().Contains("Default")\r
600                                           ? "bframes=" + value\r
601                                           : string.Empty;\r
602                         }\r
603                         else if (optNameToChange.Equals("subq"))\r
604                         {\r
605                             string value = drop_subpixelMotionEstimation.SelectedItem.ToString();\r
606                             string[] val = value.Split(':');\r
607                             thisOpt = !drop_subpixelMotionEstimation.SelectedItem.ToString().Contains("Default")\r
608                                           ? "subq=" + val[0]\r
609                                           : string.Empty;\r
610                         }\r
611                         else if (optNameToChange.Equals("trellis"))\r
612                         {\r
613                             switch (drop_trellis.SelectedIndex)\r
614                             {\r
615                                 case 1: // Off\r
616                                     thisOpt = "trellis=0";\r
617                                     break;\r
618                                 case 2: // Encode Only\r
619                                     thisOpt = "trellis=1";\r
620                                     break;\r
621                                 case 3: // Always\r
622                                     thisOpt = "trellis=2";\r
623                                     break;\r
624                                 default:\r
625                                     thisOpt = string.Empty;\r
626                                     break;\r
627                             }\r
628                         }\r
629                     }\r
630                 }\r
631 \r
632                 /* Construct New String for opts here */\r
633                 if (!thisOpt.Equals(string.Empty))\r
634                     changedOptString = changedOptString.Equals(string.Empty) ? thisOpt : changedOptString + ":" + thisOpt;\r
635             }\r
636 \r
637             /* Change the option string to reflect the new mod settings */\r
638             rtf_x264Query.Text = changedOptString;\r
639         }\r
640 \r
641         /// <summary>\r
642         /// Add's an option to the x264 query string.\r
643         /// Handles 2 cases.  1 Where rtf_x264Query.Text is empty, and one where there is an option with no value,\r
644         /// e.g no-fast-pskip\r
645         /// </summary>\r
646         /// <param name="optNameToChange">The Option Name to Change</param>\r
647         private void HasNoOptions(IEquatable<string> optNameToChange)\r
648         {\r
649             string colon = string.Empty;\r
650             if (rtf_x264Query.Text != string.Empty)\r
651                 colon = ":";\r
652 \r
653             string query = rtf_x264Query.Text;\r
654             if (optNameToChange.Equals("me"))\r
655             {\r
656                 switch (drop_MotionEstimationMethod.SelectedIndex)\r
657                 {\r
658                     case 1:\r
659                         query = query + colon + "me=dia";\r
660                         break;\r
661 \r
662                     case 2:\r
663                         query = query + colon + "me=hex";\r
664                         break;\r
665 \r
666                     case 3:\r
667                         query = query + colon + "me=umh";\r
668                         break;\r
669 \r
670                     case 4:\r
671                         query = query + colon + "me=esa";\r
672                         break;\r
673 \r
674                     case 5:\r
675                         query = query + colon + "me=tesa";\r
676                         break;\r
677 \r
678                     default:\r
679                         break;\r
680                 }\r
681             }\r
682             else if (optNameToChange.Equals("direct"))\r
683             {\r
684                 switch (drop_directPrediction.SelectedIndex)\r
685                 {\r
686                     case 1:\r
687                         query = query + colon + "direct=none";\r
688                         break;\r
689 \r
690                     case 2:\r
691                         query = query + colon + "direct=spatial";\r
692                         break;\r
693 \r
694                     case 3:\r
695                         query = query + colon + "direct=temporal";\r
696                         break;\r
697 \r
698                     case 4:\r
699                         query = query + colon + "direct=auto";\r
700                         break;\r
701 \r
702                     default:\r
703                         break;\r
704                 }\r
705             }\r
706             else if (optNameToChange.Equals("analyse"))\r
707             {\r
708                 switch (drop_analysis.SelectedIndex)\r
709                 {\r
710                     case 1:\r
711                         query = query + colon + "analyse=none";\r
712                         break;\r
713 \r
714                     case 2:\r
715                         query = query + colon + "analyse=some";\r
716                         break;\r
717 \r
718                     case 3:\r
719                         query = query + colon + "analyse=all";\r
720                         break;\r
721 \r
722                     default:\r
723                         break;\r
724                 }\r
725             }\r
726             else if (optNameToChange.Equals("merange"))\r
727             {\r
728                 int value = drop_MotionEstimationRange.SelectedIndex + 3;\r
729                 query = query + colon + "merange=" + value;\r
730             }\r
731             else if (optNameToChange.Equals("b-adapt"))\r
732             {\r
733                 int value = drop_adaptBFrames.SelectedIndex - 1;\r
734                 query = query + colon + "b-adapt=" + value;\r
735             }\r
736             else if (optNameToChange.Equals("deblock"))\r
737             {\r
738                 string da = drop_deblockAlpha.SelectedItem.ToString();\r
739                 string db = drop_deblockBeta.Text;\r
740 \r
741                 if (((da.Contains("Default")) && (db.Contains("Default"))) || ((da.Contains("0")) && (db.Contains("0"))))\r
742                 {\r
743                     drop_deblockBeta.SelectedItem = "Default (0)";\r
744                     drop_deblockAlpha.SelectedItem = "Default (0)";\r
745                 }\r
746                 else\r
747                 {\r
748                     if (db.Contains("Default"))\r
749                         db = "0";\r
750 \r
751                     if (da.Contains("Default"))\r
752                         da = "0";\r
753 \r
754                     query = query + colon + "deblock=" + da + "," + db;\r
755                 }\r
756             }\r
757             else if (optNameToChange.Equals("aq-strength"))\r
758             {\r
759                 if (slider_adaptiveQuantStrength.Value == 10)\r
760                     query = string.Empty;\r
761                 else\r
762                 {\r
763                     double value = slider_adaptiveQuantStrength.Value * 0.1;\r
764                     string aqs = value.ToString("f1");\r
765                     query += colon + "aq-strength=" + aqs;\r
766                 }\r
767             }\r
768             else if (optNameToChange.Equals("psy-rd"))\r
769             {\r
770                 if (slider_psyrd.Value == 10 && slider_psytrellis.Value == 0)\r
771                     query += string.Empty;\r
772                 else\r
773                 {\r
774                     double psyrd = slider_psyrd.Value * 0.1;\r
775                     double psytre = slider_psytrellis.Value * 0.05;\r
776 \r
777                     string rd = psyrd.ToString("f1");\r
778                     string rt = psytre.ToString("f2");\r
779 \r
780                     query += colon + "psy-rd=" + rd + "," + rt;\r
781                 }\r
782             }\r
783             else if (optNameToChange.Equals("b-pyramid"))\r
784             {\r
785                 switch (combo_pyrmidalBFrames.SelectedIndex)\r
786                 {\r
787                     case 0:\r
788                         break;\r
789                     case 1:\r
790                         query = query + colon + "b-pyramid=none";\r
791                         break;\r
792                     case 2:\r
793                         query = query + colon + "b-pyramid=strict";\r
794                         break;\r
795                 }\r
796             }\r
797             else if (optNameToChange.Equals("no-dct-decimate"))\r
798             {\r
799                 if (check_noDCTDecimate.CheckState == CheckState.Checked)\r
800                     query = query + colon + "no-dct-decimate=1";\r
801             }\r
802             else if (optNameToChange.Equals("8x8dct"))\r
803             {\r
804                 if (check_8x8DCT.CheckState == CheckState.Unchecked)\r
805                     query = query + colon + "8x8dct=0";\r
806             }\r
807             else if (optNameToChange.Equals("cabac"))\r
808             {\r
809                 if (check_Cabac.CheckState != CheckState.Checked)\r
810                     query = query + colon + "cabac=0";\r
811             }\r
812             else if (optNameToChange.Equals("weightp"))\r
813             {\r
814                 if (check_weightp.CheckState == CheckState.Unchecked)\r
815                     query = query + colon + "weightp=0";\r
816             }\r
817             else if (optNameToChange.Equals("ref"))\r
818             {\r
819                 if (!drop_refFrames.SelectedItem.ToString().Contains("Default"))\r
820                     query = query + colon + "ref=" + drop_refFrames.SelectedItem;\r
821             }\r
822             else if (optNameToChange.Equals("bframes"))\r
823             {\r
824                 string value = drop_bFrames.SelectedItem.ToString();\r
825                 if (!drop_bFrames.SelectedItem.ToString().Contains("Default"))\r
826                     query = query + colon + "bframes=" + value;\r
827             }\r
828             else if (optNameToChange.Equals("subq"))\r
829             {\r
830                 string value = drop_subpixelMotionEstimation.SelectedItem.ToString();\r
831                 if (!drop_subpixelMotionEstimation.SelectedItem.ToString().Contains("Default"))\r
832                 {\r
833                     string[] val = value.Split(':');\r
834                     query = query + colon + "subq=" + val[0];\r
835                 }\r
836             }\r
837             else if (optNameToChange.Equals("trellis"))\r
838             {\r
839                 switch (drop_trellis.SelectedIndex)\r
840                 {\r
841                     case 1: // Off\r
842                         query = query + colon + "trellis=0";\r
843                         break;\r
844                     case 2: // Encode Only\r
845                         query = query + colon + "trellis=1";\r
846                         break;\r
847                     case 3: // Always\r
848                         query = query + colon + "trellis=2";\r
849                         break;\r
850                     default:\r
851                         break;\r
852                 }\r
853             }\r
854 \r
855             rtf_x264Query.Text = query;\r
856         }\r
857 \r
858         /// <summary>\r
859         /// Shows and hides controls based on the values of other controls.\r
860         /// </summary>\r
861         /// <param name="sender">The Sender</param>\r
862         private void Animate(string sender)\r
863         {\r
864             /* Lots of situations to cover.\r
865                - B-frames (when 0 turn of b-frame specific stuff, when < 2 disable b-pyramid)\r
866                - CABAC (when 0 turn off trellis and psy-trel\r
867                - subme (if under 6, turn off psy-rd and psy-trel)\r
868                - trellis (if 0, turn off psy-trel)\r
869              */\r
870 \r
871             switch (sender)\r
872             {\r
873                 case "bframes":\r
874                     if (drop_bFrames.SelectedIndex == 1)\r
875                     {\r
876                         /* If the b-frame widget is at 1, the user has chosen\r
877                            not to use b-frames at all. So disable the options\r
878                            that can only be used when b-frames are enabled.        */\r
879                         combo_pyrmidalBFrames.Visible = false;\r
880                         lbl_prymidalBframes.Visible = false;\r
881                         drop_directPrediction.Visible = false;\r
882                         lbl_direct_prediction.Visible = false;\r
883 \r
884                         combo_pyrmidalBFrames.SelectedIndex = 0;\r
885                         drop_directPrediction.SelectedIndex = 0;\r
886 \r
887                         drop_adaptBFrames.Visible = false;\r
888                         lbl_adaptBFrames.Visible = false;\r
889                         drop_adaptBFrames.SelectedIndex = 0;\r
890                     }\r
891                     else if (drop_bFrames.SelectedIndex == 2)\r
892                     {\r
893                         /* Only 1 b-frame? Disable b-pyramid. */\r
894                         combo_pyrmidalBFrames.Visible = false;\r
895                         lbl_prymidalBframes.Visible = false;\r
896                         combo_pyrmidalBFrames.SelectedIndex = 0;\r
897 \r
898                         drop_directPrediction.Visible = true;\r
899                         lbl_direct_prediction.Visible = true;\r
900 \r
901                         drop_adaptBFrames.Visible = true;\r
902                         lbl_adaptBFrames.Visible = true;\r
903                     }\r
904                     else\r
905                     {\r
906                         combo_pyrmidalBFrames.Visible = true;\r
907                         lbl_prymidalBframes.Visible = true;\r
908                         drop_directPrediction.Visible = true;\r
909                         lbl_direct_prediction.Visible = true;\r
910 \r
911                         drop_adaptBFrames.Visible = true;\r
912                         lbl_adaptBFrames.Visible = true;\r
913                     }\r
914                     break;\r
915                 case "cabac":\r
916                     if (check_Cabac.Checked == false)\r
917                     {\r
918                         /* Without CABAC entropy coding, trellis doesn't run. */\r
919                         drop_trellis.Visible = false;\r
920                         drop_trellis.SelectedIndex = 0;\r
921                         lbl_trellis.Visible = false;\r
922 \r
923                         slider_psytrellis.Visible = false;\r
924                         lbl_psytrellis.Visible = false;\r
925                         slider_psytrellis.Value = 0;\r
926                     }\r
927                     else\r
928                     {\r
929                         drop_trellis.Visible = true;\r
930                         lbl_trellis.Visible = true;\r
931 \r
932                         slider_psytrellis.Visible = true;\r
933                         lbl_psytrellis.Visible = true;\r
934                     }\r
935                     break;\r
936                 case "me": // Motion Estimation\r
937                     if (drop_MotionEstimationMethod.SelectedIndex < 3)\r
938                     {\r
939                         drop_MotionEstimationRange.Visible = false;\r
940                         lbl_merange.Visible = false;\r
941                         drop_MotionEstimationRange.SelectedIndex = 0;\r
942                     }\r
943                     else\r
944                     {\r
945                         drop_MotionEstimationRange.Visible = true;\r
946                         lbl_merange.Visible = true;\r
947                     }\r
948                     break;\r
949                 case "subq": // subme\r
950                     if (drop_subpixelMotionEstimation.SelectedIndex != 0 &&\r
951                         drop_subpixelMotionEstimation.SelectedIndex < 7)\r
952                     {\r
953                         slider_psyrd.Visible = false;\r
954                         slider_psyrd.Value = 10;\r
955                         lbl_psyrd.Visible = false;\r
956 \r
957                         slider_psytrellis.Visible = false;\r
958                         slider_psytrellis.Value = 0;\r
959                         lbl_psytrellis.Visible = false;\r
960                     }\r
961                     else\r
962                     {\r
963                         slider_psyrd.Visible = true;\r
964                         lbl_psyrd.Visible = true;\r
965 \r
966                         if (drop_trellis.SelectedIndex >= 2 && check_Cabac.Checked && slider_psytrellis.Visible == false)\r
967                         {\r
968                             slider_psytrellis.Visible = true;\r
969                             lbl_psytrellis.Visible = true;\r
970                         }\r
971                     }\r
972                     break;\r
973                 case "trellis": // subme\r
974                     if (drop_trellis.SelectedIndex > 0 && drop_trellis.SelectedIndex < 2)\r
975                     {\r
976                         slider_psytrellis.Visible = false;\r
977                         slider_psytrellis.Value = 0;\r
978                         lbl_psytrellis.Visible = false;\r
979                     }\r
980                     else\r
981                     {\r
982                         if ((drop_subpixelMotionEstimation.SelectedIndex == 0 ||\r
983                              drop_subpixelMotionEstimation.SelectedIndex >= 7) && check_Cabac.Checked &&\r
984                             slider_psytrellis.Visible == false)\r
985                         {\r
986                             slider_psytrellis.Visible = true;\r
987                             lbl_psytrellis.Visible = true;\r
988                         }\r
989                     }\r
990                     break;\r
991             }\r
992         }\r
993 \r
994         /* UI Events */\r
995 \r
996         private void widgetControlChanged(object sender, EventArgs e)\r
997         {\r
998             Control changedControlName = (Control) sender;\r
999             string controlName = string.Empty;\r
1000 \r
1001             switch (changedControlName.Name.Trim())\r
1002             {\r
1003                 case "drop_refFrames":\r
1004                     controlName = "ref";\r
1005                     break;\r
1006                 case "drop_bFrames":\r
1007                     controlName = "bframes";\r
1008                     break;\r
1009                 case "drop_directPrediction":\r
1010                     controlName = "direct";\r
1011                     break;\r
1012                 case "check_weightp":\r
1013                     controlName = "weightp";\r
1014                     break;\r
1015                 case "combo_pyrmidalBFrames":\r
1016                     controlName = "b-pyramid";\r
1017                     break;\r
1018                 case "drop_MotionEstimationMethod":\r
1019                     controlName = "me";\r
1020                     break;\r
1021                 case "drop_MotionEstimationRange":\r
1022                     controlName = "merange";\r
1023                     break;\r
1024                 case "drop_subpixelMotionEstimation":\r
1025                     controlName = "subq";\r
1026                     break;\r
1027                 case "drop_analysis":\r
1028                     controlName = "analyse";\r
1029                     break;\r
1030                 case "check_8x8DCT":\r
1031                     controlName = "8x8dct";\r
1032                     break;\r
1033                 case "drop_deblockAlpha":\r
1034                     controlName = "deblock";\r
1035                     break;\r
1036                 case "drop_deblockBeta":\r
1037                     controlName = "deblock";\r
1038                     break;\r
1039                 case "drop_trellis":\r
1040                     controlName = "trellis";\r
1041                     break;\r
1042                 case "check_noDCTDecimate":\r
1043                     controlName = "no-dct-decimate";\r
1044                     break;\r
1045                 case "check_Cabac":\r
1046                     controlName = "cabac";\r
1047                     break;\r
1048                 case "slider_psyrd":\r
1049                     controlName = "psy-rd";\r
1050                     break;\r
1051                 case "slider_psytrellis":\r
1052                     controlName = "psy-rd";\r
1053                     break;\r
1054                 case "slider_adaptiveQuantStrength":\r
1055                     controlName = "aq-strength";\r
1056                     break;\r
1057                 case "drop_adaptBFrames":\r
1058                     controlName = "b-adapt";\r
1059                     break;\r
1060             }\r
1061             OnX264WidgetChange(controlName);\r
1062         }\r
1063 \r
1064         private void rtf_x264Query_TextChanged(object sender, EventArgs e)\r
1065         {\r
1066             if (rtf_x264Query.Text.EndsWith("\n"))\r
1067             {\r
1068                 string query = rtf_x264Query.Text.Replace("\n", string.Empty);\r
1069                 Reset2Defaults();\r
1070                 rtf_x264Query.Text = query;\r
1071                 this.StandardizeOptString();\r
1072                 this.SetCurrentSettingsInPanel();\r
1073 \r
1074                 if (rtf_x264Query.Text == string.Empty)\r
1075                     Reset2Defaults();\r
1076             }\r
1077         }\r
1078 \r
1079         private void btn_reset_Click(object sender, EventArgs e)\r
1080         {\r
1081             rtf_x264Query.Text = string.Empty;\r
1082             Reset2Defaults();\r
1083         }\r
1084     }\r
1085 }