1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Handbrake.Parsing;
namespace Handbrake.Controls
{
public partial class PictureSettings : UserControl
{
private bool _preventChangingWidth, _preventChangingHeight;
private int _maxWidth, _maxHeight, _lastEncodeWidth, _lastEncodeHeight;
private double _anamorphicRatio, _displayRatio;
private Title _title;
private double storageAspect;
public event EventHandler PictureSettingsChanged;
public PictureSettings()
{
InitializeComponent();
drp_anamorphic.SelectedIndex = 1;
drp_modulus.SelectedIndex = 0;
}
/// <summary>
/// Gets or sets the source media used by this control.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Title Source
{
get { return _title; }
set
{
_title = value;
_displayRatio = ((double)_title.Resolution.Width * _title.ParVal.Width / _title.ParVal.Height) / _title.Resolution.Height;
Enabled = _title != null;
MaximumWidth = _title.Resolution.Width;
MaximumHeight = _title.Resolution.Height;
updownParWidth.Value = _title.ParVal.Width;
updownParHeight.Value = _title.ParVal.Height;
// Set the source resolution
lbl_src_res.Text = Source.Resolution.Width + " x " + Source.Resolution.Height;
// Set ratios
_anamorphicRatio = (double)Source.Resolution.Width / Source.Resolution.Height;
// Set the encode width and height
EncodeWidth = _title.Resolution.Width;
EncodeHeight = _title.Resolution.Height;
_lastEncodeWidth = _title.Resolution.Width;
_lastEncodeHeight = _title.Resolution.Height;
// Set cropping
CroppingValues = _title.AutoCropDimensions;
lbl_Aspect.Text = _title.AspectRatio.ToString();
UpdateAnamorphicValue();
}
}
/// <summary>
/// Gets or sets the resolution of the displayed video.
/// </summary>
public Size DisplayResolution { get; set; }
public int EncodeWidth { get { return (int)text_width.Value; } set { text_width.Value = value; } }
public int EncodeHeight { get { return (int)text_height.Value; } set { text_height.Value = value; } }
public int[] CroppingValues
{
get
{
return new int[4]
{
(int)crop_top.Value,
(int)crop_bottom.Value,
(int)crop_left.Value,
(int)crop_right.Value
};
}
set
{
if (value.Length != 4)
{
throw new ArgumentException("The cropping values given must have a length of 4.");
}
crop_top.Value = value[0];
crop_bottom.Value = value[1];
crop_left.Value = value[2];
crop_right.Value = value[3];
}
}
/// <summary>
/// Gets or sets the maximum allowable width of the encoded video.
/// </summary>
public int MaximumWidth
{
get { return _maxWidth; }
set
{
_maxWidth = value > 0 ? value : (Source != null ? Source.Resolution.Width : 2560);
}
}
/// <summary>
/// Gets or sets the maximum allowable height of the encoded video.
/// </summary>
public int MaximumHeight
{
get { return _maxHeight; }
set
{
_maxHeight = value > 0 ? value : (Source != null ? Source.Resolution.Height : 2560);
}
}
public void setMax()
{
}
/// <summary>
/// Updates the anamorphic value shown as the display width.
/// </summary>
private void UpdateAnamorphicValue()
{
if (_title == null || _title.ParVal.IsEmpty)
return;
// Set globally useful values
double width;
double par;
switch (drp_anamorphic.SelectedIndex)
{
case 2:
int actualWidth = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;
int source_display_width = Source.Resolution.Width * Source.ParVal.Width / Source.ParVal.Height;
int source_cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
par = ((double) text_height.Value*source_display_width/source_cropped_height)/actualWidth;
width = (actualWidth * par);
width = Math.Truncate(width);
break;
default:
{
if (drp_anamorphic.SelectedIndex == 1) // Strict
par = (double)Source.ParVal.Width / Source.ParVal.Height;
else // Custom
par = (double)updownParWidth.Value / (double)updownParHeight.Value;
// Store the latest DAR
double rawWidth = (double)text_width.Value * par;
_displayRatio = rawWidth / (double)text_height.Value;
width = (int)Math.Round(rawWidth);
break;
}
}
labelDisplaySize.Text = width + " x " + text_height.Value;
updownDisplayWidth.Value = (decimal)width;
updownParWidth.Value = (decimal)width;
updownParHeight.Value = text_width.Value;
}
/// <summary>
/// Sets the visibility of the advanced anamorphic options.
/// </summary>
/// <param name="value">Whether or not the options should be visible.</param>
private void SetCustomAnamorphicOptionsVisible(bool visible)
{
lbl_modulus.Visible = visible;
lbl_displayWidth.Visible = visible;
lbl_parWidth.Visible = visible;
lbl_parHeight.Visible = visible;
drp_modulus.Visible = visible;
updownDisplayWidth.Visible = visible;
updownParWidth.Visible = visible;
updownParHeight.Visible = visible;
}
/// <summary>
/// Gets the normalized value from one given by the user that is divisible by the number
/// set in <see cref="_modulus"/>.
/// </summary>
/// <param name="value">The value to normalize</param>
/// <returns>A number that is divisible by <see cref="_modulus"/>.</returns>
/// <remarks>
/// The way that some video codecs, such as x264, compress video is by creating "macroblocks"
/// that are seperated into defined squares, often 16x16 pixels. Because of this, if the width
/// and height of the encoded video are not each divisible by the modulus defined, video quality
/// will suffer. This method takes the supplied value and normalizes it to the nearest mutliple
/// of <see cref="_modulus"/>.
/// </remarks>
private int GetModulusValue(int value)
{
int mod = int.Parse(drp_modulus.SelectedItem.ToString());
int remainder = value % mod;
if (remainder == 0)
return value;
return remainder >= mod / 2 ? value + (mod - remainder) : value - remainder;
}
private void ApplyStrictAnamorphic()
{
if (_anamorphicRatio == 0)
return;
_preventChangingWidth = true;
_preventChangingHeight = true;
text_width.Value = _title.Resolution.Width;
text_height.Value = _title.Resolution.Height;
_preventChangingWidth = false;
_preventChangingHeight = false;
}
/// <summary>
/// Loosely anamorphs encode width and height values.
/// </summary>
private void ApplyLooseAnamorphic()
{
// Prevents DivideByZeroExceptions
if (_anamorphicRatio == 0)
return;
int actualWidth = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;
int source_cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
if (storageAspect == 0)
storageAspect = (double)actualWidth / source_cropped_height;
double hcalc = (actualWidth / storageAspect) + 0.5;
double newHeight = GetModulusValue((int)hcalc);
text_width.Value = GetModulusValue((int)text_width.Value);
text_height.Value = (decimal)newHeight;
UpdateAnamorphicValue();
}
/// <summary>
/// Anamorphs encode width and height based on the custom options specified.
/// </summary>
private void ApplyCustomAnamorphic(Control ctrlUpdated)
{
// Make sure the PAR values are set correctly
if (updownParWidth.Value == 0)
updownParWidth.Value = Source.ParVal.Width;
if (updownParHeight.Value == 0)
updownParHeight.Value = Source.ParVal.Height;
// Set various values
int parWidth = (int)updownParWidth.Value;
int parHeight = (int)updownParHeight.Value;
if (!check_KeepAR.Checked)
{
switch (ctrlUpdated.Name)
{
case "text_width":
case "updownParWidth":
case "updownParHeight":
updownDisplayWidth.Value = Math.Round(text_width.Value * parWidth / parHeight);
break;
case "updownDisplayWidth":
updownParWidth.Value = updownDisplayWidth.Value;
updownParHeight.Value = text_width.Value;
break;
}
}
else
{
switch (ctrlUpdated.Name)
{
case "updownDisplayWidth":
_preventChangingHeight = true;
text_height.Value = GetModulusValue((int)((double)updownDisplayWidth.Value / _displayRatio));
_preventChangingHeight = false;
goto case "text_width";
case "text_height":
updownDisplayWidth.Value = GetModulusValue((int)((double)text_width.Value * _anamorphicRatio * _displayRatio));
goto case "text_width";
case "text_width":
updownParWidth.Value = updownDisplayWidth.Value;
updownParHeight.Value = text_width.Value;
break;
}
}
}
private void text_width_ValueChanged(object sender, EventArgs e)
{
if (_preventChangingWidth)
return;
_preventChangingWidth = true;
if (text_width.Value > MaximumWidth)
{
text_width.Value = MaximumWidth;
}
switch (drp_anamorphic.SelectedIndex)
{
case 0:
if (check_KeepAR.Checked)
{
_preventChangingHeight = true;
decimal newHeight = text_width.Value / (decimal)_anamorphicRatio;
text_height.Value = newHeight > MaximumHeight ? MaximumHeight : newHeight;
_preventChangingHeight = false;
}
break;
case 1:
ApplyStrictAnamorphic();
break;
case 2:
ApplyLooseAnamorphic();
break;
case 3:
ApplyCustomAnamorphic((Control)sender);
break;
}
_preventChangingWidth = false;
}
private void text_height_ValueChanged(object sender, EventArgs e)
{
if (_preventChangingHeight)
return;
_preventChangingHeight = true;
if (text_height.Value > MaximumHeight)
{
text_height.Value = MaximumHeight;
}
switch (drp_anamorphic.SelectedIndex)
{
case 0:
if (check_KeepAR.Checked)
{
_preventChangingWidth = true;
decimal newWidth = text_height.Value * (decimal)_anamorphicRatio;
text_width.Value = newWidth > MaximumWidth ? MaximumWidth : newWidth;
_preventChangingWidth = false;
}
break;
case 3:
ApplyCustomAnamorphic((Control)sender);
break;
}
_preventChangingHeight = false;
}
private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)
{
switch (drp_anamorphic.SelectedIndex)
{
case 0:
text_width.Enabled = true;
text_height.Enabled = true;
check_KeepAR.Enabled = true;
SetCustomAnamorphicOptionsVisible(false);
labelStaticDisplaySize.Visible = false;
labelDisplaySize.Visible = false;
check_KeepAR.Checked = true;
break;
case 1:
text_width.Enabled = false;
text_height.Enabled = false;
check_KeepAR.Enabled = false;
SetCustomAnamorphicOptionsVisible(false);
labelStaticDisplaySize.Visible = true;
labelDisplaySize.Visible = true;
check_KeepAR.Checked = true;
break;
case 2:
text_width.Enabled = true;
text_height.Enabled = false;
check_KeepAR.Enabled = false;
SetCustomAnamorphicOptionsVisible(false);
labelStaticDisplaySize.Visible = true;
labelDisplaySize.Visible = true;
check_KeepAR.Checked = true;
break;
case 3:
text_width.Enabled = true;
text_height.Enabled = true;
check_KeepAR.Enabled = true;
SetCustomAnamorphicOptionsVisible(true);
labelStaticDisplaySize.Visible = true;
labelDisplaySize.Visible = true;
check_KeepAR.Checked = true;
break;
}
UpdateAnamorphicValue();
}
private void check_KeepAR_CheckedChanged(object sender, EventArgs e)
{
if (drp_anamorphic.SelectedIndex != 3)
{
if (check_KeepAR.Checked)
{
text_width_ValueChanged(this, new EventArgs());
}
}
else
{
updownParWidth.Enabled = !check_KeepAR.Checked;
updownParHeight.Enabled = !check_KeepAR.Checked;
}
}
private void crop_ValueChanged(object sender, EventArgs e)
{
text_width_ValueChanged(this, new EventArgs());
}
private void check_autoCrop_CheckedChanged(object sender, EventArgs e)
{
crop_top.Enabled = check_customCrop.Checked;
crop_bottom.Enabled = check_customCrop.Checked;
crop_left.Enabled = check_customCrop.Checked;
crop_right.Enabled = check_customCrop.Checked;
}
private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)
{
text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
}
}
}
|