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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AudioTrack.cs" company="HandBrake Project (http://handbrake.fr)">
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
// Model of a HandBrake Audio Track and it's associated behaviours.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Services.Encode.Model.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Security;
using HandBrake.ApplicationServices.Interop;
using HandBrake.ApplicationServices.Interop.Model;
using HandBrake.ApplicationServices.Interop.Model.Encoding;
using HandBrake.ApplicationServices.Utilities;
using HandBrakeWPF.Model.Audio;
using HandBrakeWPF.Services.Scan.Model;
using HandBrakeWPF.Utilities;
using Newtonsoft.Json;
/// <summary>
/// Model of a HandBrake Audio Track and it's associated behaviours.
/// </summary>
public class AudioTrack : PropertyChangedBase
{
private int bitrate;
private double drc;
private HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder encoder;
private int gain;
private string mixDown;
private double sampleRate;
[NonSerialized]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
private Audio scannedTrack;
private bool isDefault;
private IEnumerable<int> bitrates;
private IEnumerable<double> encoderQualityValues;
private HandBrakeWPF.Services.Encode.Model.Models.AudioEncoderRateType encoderRateType;
private double? quality;
/// <summary>
/// Initializes a new instance of the <see cref = "AudioTrack" /> class.
/// </summary>
public AudioTrack()
{
// Default Values
this.Encoder = HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.ffaac;
this.MixDown = HandBrakeEncoderHelpers.Mixdowns.FirstOrDefault(m => m.ShortName == "dpl2")?.ShortName;
this.SampleRate = 48;
this.Bitrate = 160;
this.DRC = 0;
this.ScannedTrack = new Audio();
this.TrackName = string.Empty;
// Setup Backing Properties
this.EncoderRateType = HandBrakeWPF.Services.Encode.Model.Models.AudioEncoderRateType.Bitrate;
this.SetupLimits();
}
/// <summary>
/// Initializes a new instance of the <see cref="AudioTrack"/> class.
/// Copy Constructor
/// </summary>
/// <param name="track">
/// The track.
/// </param>
/// <param name="setScannedTrack">
/// The set Scanned Track.
/// </param>
public AudioTrack(AudioTrack track, bool setScannedTrack)
{
this.bitrate = track.Bitrate;
this.drc = track.DRC;
this.encoder = track.Encoder;
this.gain = track.Gain;
this.mixDown = track.MixDown;
this.sampleRate = track.SampleRate;
if (setScannedTrack)
{
this.scannedTrack = track.ScannedTrack ?? new Audio();
}
this.TrackName = track.TrackName;
this.Quality = track.Quality;
// Setup Backing Properties
this.encoderRateType = track.EncoderRateType;
this.SetupLimits();
}
/// <summary>
/// Initializes a new instance of the <see cref="AudioTrack"/> class.
/// Create a track from a behaviour track.
/// </summary>
/// <param name="track">
/// The Behavior track
/// </param>
/// <param name="sourceTrack">
/// The source track we are dealing with.
/// </param>
/// <param name="fallback">
/// An encoder to fall back to.
/// </param>
public AudioTrack(AudioBehaviourTrack track, Audio sourceTrack, AudioEncoder fallback)
{
AudioEncoder chosenEncoder = track.Encoder;
HBAudioEncoder encoderInfo = HandBrakeEncoderHelpers.GetAudioEncoder(EnumHelper<AudioEncoder>.GetShortName(track.Encoder));
if (track.IsPassthru && (sourceTrack.Codec & encoderInfo.Id) == 0)
{
chosenEncoder = fallback;
}
this.scannedTrack = sourceTrack;
this.drc = track.DRC;
this.encoder = chosenEncoder;
this.gain = track.Gain;
this.mixDown = track.MixDown != null ? track.MixDown.ShortName : "dpl2";
this.sampleRate = track.SampleRate;
this.encoderRateType = track.EncoderRateType;
this.quality = track.Quality;
this.bitrate = track.Bitrate;
this.SetupLimits();
}
#region Track Properties
/// <summary>
/// Gets or sets Dynamic Range Compression
/// </summary>
public double DRC
{
get
{
return this.drc;
}
set
{
if (!Equals(value, this.drc))
{
this.drc = value;
this.NotifyOfPropertyChange(() => this.DRC);
}
}
}
/// <summary>
/// Gets or sets the Gain for the audio track
/// </summary>
public int Gain
{
get
{
return this.gain;
}
set
{
if (!Equals(value, this.gain))
{
this.gain = value;
this.NotifyOfPropertyChange(() => this.Gain);
}
}
}
/// <summary>
/// Gets or sets Audio Mixdown (ShortName)
/// </summary>
public string MixDown
{
get
{
return this.IsPassthru ? null : this.mixDown;
}
set
{
if (!object.Equals(this.mixDown, value))
{
this.mixDown = value;
this.NotifyOfPropertyChange(() => this.MixDown);
this.SetupLimits();
}
}
}
/// <summary>
/// Gets or sets Audio Encoder
/// </summary>
public AudioEncoder Encoder
{
get
{
return this.encoder;
}
set
{
if (object.Equals(this.encoder, value))
{
return;
}
this.encoder = value;
this.NotifyOfPropertyChange(() => this.Encoder);
this.NotifyOfPropertyChange(() => this.IsPassthru);
this.NotifyOfPropertyChange(() => this.IsBitrateVisible);
this.NotifyOfPropertyChange(() => this.IsQualityVisible);
this.NotifyOfPropertyChange(() => this.IsRateTypeVisible);
this.NotifyOfPropertyChange(() => this.TrackReference);
this.GetDefaultMixdownIfNull();
this.SetupLimits();
// Refresh the available encoder rate types.
this.NotifyOfPropertyChange(() => this.AudioEncoderRateTypes);
if (!this.AudioEncoderRateTypes.Contains(this.EncoderRateType))
{
this.EncoderRateType = HandBrakeWPF.Services.Encode.Model.Models.AudioEncoderRateType.Bitrate; // Default to bitrate.
}
}
}
/// <summary>
/// Gets or sets Audio SampleRate
/// </summary>
public double SampleRate
{
get
{
return this.sampleRate;
}
set
{
this.sampleRate = value;
this.NotifyOfPropertyChange(() => this.SampleRate);
this.SetupLimits();
}
}
/// <summary>
/// Gets or sets the encoder rate type.
/// </summary>
public AudioEncoderRateType EncoderRateType
{
get
{
return this.encoderRateType;
}
set
{
this.encoderRateType = value;
this.SetupLimits();
this.NotifyOfPropertyChange(() => this.EncoderRateType);
this.NotifyOfPropertyChange(() => this.IsBitrateVisible);
this.NotifyOfPropertyChange(() => this.IsQualityVisible);
if (!this.Quality.HasValue)
{
HBAudioEncoder hbAudioEncoder = HandBrakeEncoderHelpers.GetAudioEncoder(EnumHelper<HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder>.GetShortName(this.Encoder));
this.Quality = HandBrakeEncoderHelpers.GetDefaultQuality(hbAudioEncoder);
}
}
}
/// <summary>
/// Gets or sets Audio Bitrate
/// </summary>
public int Bitrate
{
get
{
return this.bitrate;
}
set
{
this.bitrate = value;
this.NotifyOfPropertyChange(() => this.Bitrate);
}
}
/// <summary>
/// Gets or sets Audio quality
/// </summary>
public double? Quality
{
get
{
return this.quality;
}
set
{
this.quality = value;
this.NotifyOfPropertyChange(() => this.quality);
}
}
/// <summary>
/// Gets or sets the track name.
/// </summary>
public string TrackName { get; set; }
#endregion
/// <summary>
/// Gets AudioEncoderDisplayValue.
/// </summary>
[JsonIgnore]
public string AudioEncoderDisplayValue
{
get
{
return EnumHelper<HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder>.GetDisplay(this.Encoder);
}
}
/// <summary>
/// Gets the The UI display value for bit rate
/// </summary>
[JsonIgnore]
public string BitRateDisplayValue
{
get
{
if (this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.Ac3Passthrough || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.DtsPassthrough
|| this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.DtsHDPassthrough)
{
return "Auto";
}
return this.Bitrate.ToString();
}
}
/// <summary>
/// Gets or sets a value indicating whether is default.
/// TODO - Can this be removed? May have been added as a quick fix for a styling quirk.
/// </summary>
[JsonIgnore]
public bool IsDefault
{
get
{
return this.isDefault;
}
set
{
this.isDefault = value;
}
}
/// <summary>
/// Gets or sets the The UI display value for sample rate
/// </summary>
[JsonIgnore]
public string SampleRateDisplayValue
{
get
{
return this.SampleRate == 0 ? "Auto" : this.SampleRate.ToString(CultureInfo.InvariantCulture);
}
set
{
// TODO change this to be a converted field
if (string.IsNullOrEmpty(value))
{
return;
}
double samplerate;
double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out samplerate);
this.SampleRate = samplerate;
}
}
/// <summary>
/// Gets or sets the Scanned Audio Tracks
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Audio ScannedTrack
{
get
{
return this.scannedTrack;
}
set
{
this.scannedTrack = value;
this.NotifyOfPropertyChange(() => this.ScannedTrack);
this.NotifyOfPropertyChange(() => this.TrackReference);
this.GetDefaultMixdownIfNull();
}
}
/// <summary>
/// Gets the Audio Track Name
/// </summary>
[JsonIgnore]
public int? Track
{
get
{
if (this.ScannedTrack != null)
{
return this.ScannedTrack.TrackNumber;
}
return null;
}
}
/// <summary>
/// Gets a value indicating whether IsPassthru.
/// </summary>
[JsonIgnore]
public bool IsPassthru
{
get
{
if (this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.Ac3Passthrough || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.DtsPassthrough
|| this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.DtsHDPassthrough || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.AacPassthru
|| this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.Mp3Passthru || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.Passthrough ||
this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.EAc3Passthrough || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.TrueHDPassthrough
|| this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.FlacPassthru)
{
return true;
}
return false;
}
}
/// <summary>
/// Gets the bitrates.
/// </summary>
[JsonIgnore]
public IEnumerable<int> Bitrates
{
get
{
return this.bitrates;
}
}
/// <summary>
/// Gets the quality compression values.
/// </summary>
[JsonIgnore]
public IEnumerable<double> EncoderQualityValues
{
get
{
return this.encoderQualityValues;
}
}
/// <summary>
/// Gets the audio encoder rate types.
/// </summary>
[JsonIgnore]
public IEnumerable<AudioEncoderRateType> AudioEncoderRateTypes
{
get
{
IList<HandBrakeWPF.Services.Encode.Model.Models.AudioEncoderRateType> types = EnumHelper<HandBrakeWPF.Services.Encode.Model.Models.AudioEncoderRateType>.GetEnumList().ToList();
HBAudioEncoder hbaenc = HandBrakeEncoderHelpers.GetAudioEncoder(EnumHelper<HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder>.GetShortName(this.Encoder));
if (hbaenc == null || !hbaenc.SupportsQuality)
{
types.Remove(HandBrakeWPF.Services.Encode.Model.Models.AudioEncoderRateType.Quality);
}
return types;
}
}
/// <summary>
/// Gets a value indicating whether can set bitrate.
/// </summary>
[JsonIgnore]
public bool IsBitrateVisible
{
get
{
if (this.IsPassthru || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.ffflac || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.ffflac24)
{
return false;
}
return Equals(this.EncoderRateType, HandBrakeWPF.Services.Encode.Model.Models.AudioEncoderRateType.Bitrate);
}
}
/// <summary>
/// Gets a value indicating whether is quality visible.
/// </summary>
[JsonIgnore]
public bool IsQualityVisible
{
get
{
if (this.IsPassthru || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.ffflac || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.ffflac24)
{
return false;
}
return Equals(this.EncoderRateType, HandBrakeWPF.Services.Encode.Model.Models.AudioEncoderRateType.Quality);
}
}
/// <summary>
/// Gets a value indicating whether is rate type visible.
/// </summary>
[JsonIgnore]
public bool IsRateTypeVisible
{
get
{
if (this.IsPassthru || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.ffflac || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.ffflac24)
{
return false;
}
return true;
}
}
/// <summary>
/// Gets a value indicating whether IsLossless.
/// </summary>
[JsonIgnore]
public bool IsLossless
{
get
{
return this.IsPassthru || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.ffflac || this.Encoder == HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder.ffflac24;
}
}
/// <summary>
/// Gets TrackReference.
/// </summary>
[JsonIgnore]
public AudioTrack TrackReference
{
get { return this; }
}
#region Handler Methods
/// <summary>
/// The setup limits.
/// </summary>
private void SetupLimits()
{
this.SetupBitrateLimits();
this.SetupQualityCompressionLimits();
}
/// <summary>
/// The calculate bitrate limits.
/// </summary>
private void SetupBitrateLimits()
{
// Base set of bitrates available.
List<int> audioBitrates = HandBrakeEncoderHelpers.AudioBitrates;
// Defaults
int max = 256;
int low = 32;
// Based on the users settings, find the high and low bitrates.
HBAudioEncoder hbaenc = HandBrakeEncoderHelpers.GetAudioEncoder(EnumHelper<HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder>.GetShortName(this.Encoder));
HBRate rate = HandBrakeEncoderHelpers.AudioSampleRates.FirstOrDefault(t => t.Name == this.SampleRate.ToString(CultureInfo.InvariantCulture));
HBMixdown mixdown = this.mixDown != null ? HandBrakeEncoderHelpers.GetMixdown(this.mixDown) : HandBrakeEncoderHelpers.GetMixdown("dpl2");
BitrateLimits limits = HandBrakeEncoderHelpers.GetBitrateLimits(hbaenc, rate != null ? rate.Rate : 48000, mixdown);
if (limits != null)
{
max = limits.High;
low = limits.Low;
}
// Return the subset of available bitrates.
List<int> subsetBitrates = audioBitrates.Where(b => b <= max && b >= low).ToList();
this.bitrates = subsetBitrates;
this.NotifyOfPropertyChange(() => this.Bitrates);
// If the subset does not contain the current bitrate, request the default.
if (!subsetBitrates.Contains(this.Bitrate))
{
this.Bitrate = HandBrakeEncoderHelpers.GetDefaultBitrate(hbaenc, rate != null ? rate.Rate : 48000, mixdown);
}
}
/// <summary>
/// The setup quality compression limits.
/// </summary>
private void SetupQualityCompressionLimits()
{
HBAudioEncoder hbAudioEncoder = HandBrakeEncoderHelpers.GetAudioEncoder(EnumHelper<HandBrakeWPF.Services.Encode.Model.Models.AudioEncoder>.GetShortName(this.Encoder));
if (hbAudioEncoder.SupportsQuality)
{
RangeLimits limits = null;
if (hbAudioEncoder.SupportsQuality)
{
limits = hbAudioEncoder.QualityLimits;
}
if (limits != null)
{
double value = limits.Ascending ? limits.Low : limits.High;
List<double> values = new List<double> { value };
if (limits.Ascending)
{
while (value < limits.High)
{
value += limits.Granularity;
values.Add(value);
}
}
else
{
while (value > limits.Low)
{
value -= limits.Granularity;
values.Add(value);
}
}
this.encoderQualityValues = values;
}
else
{
this.encoderQualityValues = new List<double>();
}
}
else
{
this.encoderQualityValues = new List<double>();
}
// Default the audio quality value if it's out of range.
if (Equals(this.EncoderRateType, HandBrakeWPF.Services.Encode.Model.Models.AudioEncoderRateType.Quality))
{
if (this.Quality.HasValue && !this.encoderQualityValues.Contains(this.Quality.Value))
{
this.Quality = HandBrakeEncoderHelpers.GetDefaultQuality(hbAudioEncoder);
}
}
this.NotifyOfPropertyChange(() => this.EncoderQualityValues);
}
/// <summary>
/// Set the default mixdown when the mixdown is null or "none"
/// </summary>
private void GetDefaultMixdownIfNull()
{
if (this.ScannedTrack == null)
{
return;
}
HBAudioEncoder aencoder = HandBrakeEncoderHelpers.GetAudioEncoder(EnumHelper<AudioEncoder>.GetShortName(this.encoder));
HBMixdown currentMixdown = HandBrakeEncoderHelpers.GetMixdown(this.mixDown);
HBMixdown sanitisedMixdown = HandBrakeEncoderHelpers.SanitizeMixdown(currentMixdown, aencoder, (uint)this.ScannedTrack.ChannelLayout);
HBMixdown defaultMixdown = HandBrakeEncoderHelpers.GetDefaultMixdown(aencoder, (uint)this.ScannedTrack.ChannelLayout);
if (this.mixDown == null || this.mixDown == "none")
{
this.MixDown = defaultMixdown.ShortName;
}
else if (sanitisedMixdown != null)
{
this.MixDown = sanitisedMixdown.ShortName;
}
}
#endregion
}
}
|