summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeEncoderHelpers.cs
blob: e4312e451f05e7615d103258013b8be59365a6a1 (plain)
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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HandBrakeEncoderHelpers.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>
//   The encoders.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrake.ApplicationServices.Interop
{
    using System.Collections.Generic;
    using System.Linq;

    using HandBrake.ApplicationServices.Interop.HbLib;
    using HandBrake.ApplicationServices.Interop.Helpers;
    using HandBrake.ApplicationServices.Interop.Model;
    using HandBrake.ApplicationServices.Interop.Model.Encoding;

    /// <summary>
    /// The encoders.
    /// </summary>
    public static class HandBrakeEncoderHelpers
    {
        /// <summary>
        /// The audio encoders.
        /// </summary>
        private static List<HBAudioEncoder> audioEncoders;

        /// <summary>
        /// The video encoders.
        /// </summary>
        private static List<HBVideoEncoder> videoEncoders;

        /// <summary>
        /// Video framerates in pts.
        /// </summary>
        private static List<HBRate> videoFramerates;

        /// <summary>
        /// List of HandBrake mixdowns.
        /// </summary>
        private static List<HBMixdown> mixdowns;

        /// <summary>
        /// List of HandBrake containers.
        /// </summary> 
        private static List<HBContainer> containers;

        /// <summary>
        /// The audio bitrates.
        /// </summary>
        private static List<int> audioBitrates;

        /// <summary>
        /// Audio sample rates in Hz.
        /// </summary>
        private static List<HBRate> audioSampleRates;

        /// <summary>
        /// Initializes static members of the HandBrakeEncoderHelpers class.
        /// </summary>
        static HandBrakeEncoderHelpers()
        {
            HandBrakeUtils.EnsureGlobalInit();
        }

        /// <summary>
        /// Gets a list of supported audio encoders.
        /// </summary>
        public static List<HBAudioEncoder> AudioEncoders
        {
            get
            {
                if (audioEncoders == null)
                {
                    audioEncoders = InteropUtilities.ToListFromIterator<hb_encoder_s, HBAudioEncoder>(HBFunctions.hb_audio_encoder_get_next, HandBrakeUnitConversionHelpers.NativeToAudioEncoder);
                }

                return audioEncoders;
            }
        }

        /// <summary>
        /// Gets a list of supported video encoders.
        /// </summary>
        public static List<HBVideoEncoder> VideoEncoders
        {
            get
            {
                if (videoEncoders == null)
                {
                    videoEncoders = InteropUtilities.ToListFromIterator<hb_encoder_s, HBVideoEncoder>(HBFunctions.hb_video_encoder_get_next, HandBrakeUnitConversionHelpers.NativeToVideoEncoder);
                }

                return videoEncoders;
            }
        }

        /// <summary>
        /// Gets a list of supported video framerates (in pts).
        /// </summary>
        public static List<HBRate> VideoFramerates
        {
            get
            {
                if (videoFramerates == null)
                {
                    videoFramerates = InteropUtilities.ToListFromIterator<hb_rate_s, HBRate>(HBFunctions.hb_video_framerate_get_next, HandBrakeUnitConversionHelpers.NativeToRate);
                }

                return videoFramerates;
            }
        }

        /// <summary>
        /// Gets a list of supported mixdowns.
        /// </summary>
        public static List<HBMixdown> Mixdowns
        {
            get
            {
                if (mixdowns == null)
                {
                    mixdowns = InteropUtilities.ToListFromIterator<hb_mixdown_s, HBMixdown>(HBFunctions.hb_mixdown_get_next, HandBrakeUnitConversionHelpers.NativeToMixdown);
                }

                return mixdowns;
            }
        }

        /// <summary>
        /// Gets a list of supported audio bitrates.
        /// </summary>
        public static List<int> AudioBitrates
        {
            get
            {
                if (audioBitrates == null)
                {
                    audioBitrates = InteropUtilities.ToListFromIterator<hb_rate_s, int>(HBFunctions.hb_audio_bitrate_get_next, b => b.rate);
                }

                return audioBitrates;
            }
        }

        /// <summary>
        /// Gets a list of supported audio sample rates (in Hz).
        /// </summary>
        public static List<HBRate> AudioSampleRates
        {
            get
            {
                if (audioSampleRates == null)
                {
                    audioSampleRates = InteropUtilities.ToListFromIterator<hb_rate_s, HBRate>(HBFunctions.hb_audio_samplerate_get_next, HandBrakeUnitConversionHelpers.NativeToRate);
                }

                return audioSampleRates;
            }
        }

        /// <summary>
        /// Gets a list of supported containers.
        /// </summary>
        public static List<HBContainer> Containers
        {
            get
            {
                if (containers == null)
                {
                    containers = InteropUtilities.ToListFromIterator<hb_container_s, HBContainer>(HBFunctions.hb_container_get_next, HandBrakeUnitConversionHelpers.NativeToContainer);
                }

                return containers;
            }
        }

        /// <summary>
        /// Gets a value indicating whether SRT subtitles can be burnt in.
        /// </summary>
        public static bool CanBurnSrt
        {
            get
            {
                return HBFunctions.hb_subtitle_can_burn((int)hb_subtitle_s_subsource.SRTSUB) > 0;
            }
        }

        /// <summary>
        /// Gets the audio encoder with the specified short name.
        /// </summary>
        /// <param name="shortName">
        /// The name of the audio encoder.
        /// </param>
        /// <returns>
        /// The requested audio encoder.
        /// </returns>
        public static HBAudioEncoder GetAudioEncoder(string shortName)
        {
            return AudioEncoders.SingleOrDefault(e => e.ShortName == shortName);
        }

        /// <summary>
        /// Gets the audio encoder with the specified codec ID.
        /// </summary>
        /// <param name="codecId">
        /// The ID of the audio encoder.
        /// </param>
        /// <returns>
        /// The requested audio encoder.
        /// </returns>
        public static HBAudioEncoder GetAudioEncoder(int codecId)
        {
            return AudioEncoders.SingleOrDefault(e => e.Id == codecId);
        }

        /// <summary>
        /// Gets the video encoder with the specified short name.
        /// </summary>
        /// <param name="shortName">
        /// The name of the video encoder.
        /// </param>
        /// <returns>
        /// The requested video encoder.
        /// </returns>
        public static HBVideoEncoder GetVideoEncoder(string shortName)
        {
            return VideoEncoders.SingleOrDefault(e => e.ShortName == shortName);
        }

        /// <summary>
        /// Gets the mixdown with the specified short name.
        /// </summary>
        /// <param name="shortName">
        /// The name of the mixdown.
        /// </param>
        /// <returns>
        /// The requested mixdown.
        /// </returns>
        public static HBMixdown GetMixdown(string shortName)
        {
            return Mixdowns.SingleOrDefault(m => m.ShortName == shortName);
        }

        /// <summary>
        /// Gets the container with the specified short name.
        /// </summary>
        /// <param name="shortName">
        /// The name of the container.
        /// </param>
        /// <returns>
        /// The requested container.
        /// </returns>
        public static HBContainer GetContainer(string shortName)
        {
            return Containers.SingleOrDefault(c => c.ShortName == shortName);
        }

        /// <summary>
        /// Returns true if the subtitle source type can be set to forced only.
        /// </summary>
        /// <param name="source">
        /// The subtitle source type (SSA, VobSub, etc)
        /// </param>
        /// <returns>
        /// True if the subtitle source type can be set to forced only.
        /// </returns>
        public static bool SubtitleCanSetForcedOnly(int source)
        {
            return HBFunctions.hb_subtitle_can_force(source) > 0;
        }

        /// <summary>
        /// Returns true if the subtitle source type can be burned in.
        /// </summary>
        /// <param name="source">
        /// The subtitle source type (SSA, VobSub, etc)
        /// </param>
        /// <returns>
        /// True if the subtitle source type can be burned in.
        /// </returns>
        public static bool SubtitleCanBurn(int source)
        {
            return HBFunctions.hb_subtitle_can_burn(source) > 0;
        }

        /// <summary>
        /// Returns true if the subtitle type can be passed through using the given muxer.
        /// </summary>
        /// <param name="subtitleSourceType">
        /// The subtitle source type (SSA, VobSub, etc)
        /// </param>
        /// <param name="muxer">
        /// The ID of the muxer.
        /// </param>
        /// <returns>
        /// True if the subtitle type can be passed through with the given muxer.
        /// </returns>
        public static bool SubtitleCanPassthrough(int subtitleSourceType, int muxer)
        {
            return HBFunctions.hb_subtitle_can_pass(subtitleSourceType, muxer) > 0;
        }

        /// <summary>
        /// Gets the subtitle source type's name.
        /// </summary>
        /// <param name="source">
        /// The subtitle source type (SSA, VobSub, etc).
        /// </param>
        /// <returns>
        /// The name of the subtitle source.
        /// </returns>
        public static string GetSubtitleSourceName(int source)
        {
            switch ((hb_subtitle_s_subsource)source)
            {
                case hb_subtitle_s_subsource.CC608SUB:
                    return "CC608";
                case hb_subtitle_s_subsource.CC708SUB:
                    return "CC708";
                case hb_subtitle_s_subsource.SRTSUB:
                    return "SRT";
                case hb_subtitle_s_subsource.SSASUB:
                    return "SSA";
                case hb_subtitle_s_subsource.TX3GSUB:
                    return "TX3G";
                case hb_subtitle_s_subsource.UTF8SUB:
                    return "UTF8";
                case hb_subtitle_s_subsource.VOBSUB:
                    return "VobSub";
                case hb_subtitle_s_subsource.PGSSUB:
                    return "PGS";
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// Determines if the given encoder is compatible with the given track.
        /// </summary>
        /// <param name="codecId">
        /// The codec Id.
        /// </param>
        /// <param name="encoder">
        /// The encoder to examine.
        /// </param>
        /// <returns>
        /// True if the given encoder is comatible with the given audio track.
        /// </returns>
        /// <remarks>
        /// Only works with passthrough encoders.
        /// </remarks>
        public static bool AudioEncoderIsCompatible(int codecId, HBAudioEncoder encoder)
        {
            return (codecId & encoder.Id) > 0;
        }

        /// <summary>
        /// Determines if the given mixdown supports the given channel layout.
        /// </summary>
        /// <param name="mixdown">
        /// The mixdown to evaluate.
        /// </param>
        /// <param name="layout">
        /// The channel layout to evaluate.
        /// </param>
        /// <returns>
        /// True if the mixdown supports the given channel layout.
        /// </returns>
        public static bool MixdownHasRemixSupport(HBMixdown mixdown, ulong layout)
        {
            return HBFunctions.hb_mixdown_has_remix_support(mixdown.Id, layout) > 0;
        }

        /// <summary>
        /// Determines if the given encoder supports the given mixdown.
        /// </summary>
        /// <param name="mixdown">
        /// The mixdown to evaluate.
        /// </param>
        /// <param name="encoder">
        /// The encoder to evaluate.
        /// </param>
        /// <returns>
        /// True if the encoder supports the mixdown.
        /// </returns>
        public static bool MixdownHasCodecSupport(HBMixdown mixdown, HBAudioEncoder encoder)
        {
            return HBFunctions.hb_mixdown_has_codec_support(mixdown.Id, (uint)encoder.Id) > 0;
        }

        /// <summary>
        /// Determines if a mixdown is available for a given track and encoder.
        /// </summary>
        /// <param name="mixdown">
        /// The mixdown to evaluate.
        /// </param>
        /// <param name="encoder">
        /// The encoder to evaluate.
        /// </param>
        /// <param name="channelLayout">channel layout of the source track</param>
        /// <returns>True if available.</returns>
        public static bool MixdownIsSupported(HBMixdown mixdown, HBAudioEncoder encoder, int channelLayout)
        {
            return HBFunctions.hb_mixdown_is_supported(mixdown.Id, (uint)encoder.Id, (uint)channelLayout) > 0;
        }

        /// <summary>
        /// Determines if DRC can be applied to the given track with the given encoder.
        /// </summary>
        /// <param name="trackNumber">
        /// The track Number.
        /// </param>
        /// <param name="encoder">
        /// The encoder to use for DRC.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <returns>
        /// True if DRC can be applied to the track with the given encoder.
        /// </returns>
        public static bool CanApplyDrc(int trackNumber, HBAudioEncoder encoder, int title)
        {
            return HBFunctions.hb_audio_can_apply_drc2(HandBrakeInstanceManager.LastScanHandle, title, trackNumber, encoder.Id) > 0;
        }

        /// <summary>
        /// Determines if the given input audio codec can be passed through.
        /// </summary>
        /// <param name="codecId">
        /// The input codec to consider.
        /// </param>
        /// <returns>
        /// True if the codec can be passed through.
        /// </returns>
        public static bool CanPassthroughAudio(int codecId)
        {
            return (codecId & NativeConstants.HB_ACODEC_PASS_MASK) > 0;
        }

        /// <summary>
        /// Sanitizes a mixdown given the output codec and input channel layout.
        /// </summary>
        /// <param name="mixdown">
        /// The desired mixdown.
        /// </param>
        /// <param name="encoder">
        /// The output encoder to be used.
        /// </param>
        /// <param name="layout">
        /// The input channel layout.
        /// </param>
        /// <returns>
        /// A sanitized mixdown value.
        /// </returns>
        public static HBMixdown SanitizeMixdown(HBMixdown mixdown, HBAudioEncoder encoder, ulong layout)
        {
            int sanitizedMixdown = HBFunctions.hb_mixdown_get_best((uint)encoder.Id, layout, mixdown.Id);
            if (sanitizedMixdown != -1)
            {
                return Mixdowns.Single(m => m.Id == sanitizedMixdown);
            }
            return Mixdowns.FirstOrDefault(); // "none"
        }

        /// <summary>
        /// Gets the default mixdown for the given audio encoder and channel layout.
        /// </summary>
        /// <param name="encoder">
        /// The output codec to be used.
        /// </param>
        /// <param name="layout">
        /// The input channel layout.
        /// </param>
        /// <returns>
        /// The default mixdown for the given codec and channel layout.
        /// </returns>
        public static HBMixdown GetDefaultMixdown(HBAudioEncoder encoder, ulong layout)
        {
            int defaultMixdown = HBFunctions.hb_mixdown_get_default((uint)encoder.Id, layout);
            return Mixdowns.Single(m => m.Id == defaultMixdown);
        }

        /// <summary>
        /// Gets the bitrate limits for the given audio codec, sample rate and mixdown.
        /// </summary>
        /// <param name="encoder">
        /// The audio encoder used.
        /// </param>
        /// <param name="sampleRate">
        /// The sample rate used (Hz).
        /// </param>
        /// <param name="mixdown">
        /// The mixdown used.
        /// </param>
        /// <returns>
        /// Limits on the audio bitrate for the given settings.
        /// </returns>
        public static BitrateLimits GetBitrateLimits(HBAudioEncoder encoder, int sampleRate, HBMixdown mixdown)
        {
            int low = 0;
            int high = 0;

            HBFunctions.hb_audio_bitrate_get_limits((uint)encoder.Id, sampleRate, mixdown.Id, ref low, ref high);

            return new BitrateLimits(low, high);
        }

        /// <summary>
        /// Gets the video quality limits for the given video codec.
        /// </summary>
        /// <param name="encoder">
        /// The video encoder to check.
        /// </param>
        /// <returns>
        /// Limits on the video quality for the encoder.
        /// </returns>
        public static VideoQualityLimits GetVideoQualityLimits(HBVideoEncoder encoder)
        {
            float low = 0;
            float high = 0;
            float granularity = 0;
            int direction = 0;

            HBFunctions.hb_video_quality_get_limits((uint)encoder.Id, ref low, ref high, ref granularity, ref direction);

            return new VideoQualityLimits(low, high, granularity, direction == 0);
        }

        /// <summary>
        /// Sanitizes an audio bitrate given the output codec, sample rate and mixdown.
        /// </summary>
        /// <param name="audioBitrate">
        /// The desired audio bitrate.
        /// </param>
        /// <param name="encoder">
        /// The output encoder to be used.
        /// </param>
        /// <param name="sampleRate">
        /// The output sample rate to be used.
        /// </param>
        /// <param name="mixdown">
        /// The mixdown to be used.
        /// </param>
        /// <returns>
        /// A sanitized audio bitrate.
        /// </returns>
        public static int SanitizeAudioBitrate(int audioBitrate, HBAudioEncoder encoder, int sampleRate, HBMixdown mixdown)
        {
            return HBFunctions.hb_audio_bitrate_get_best((uint)encoder.Id, audioBitrate, sampleRate, mixdown.Id);
        }

        /// <summary>
        /// Gets the default audio bitrate for the given parameters.
        /// </summary>
        /// <param name="encoder">
        /// The encoder to use.
        /// </param>
        /// <param name="sampleRate">
        /// The sample rate to use.
        /// </param>
        /// <param name="mixdown">
        /// The mixdown to use.
        /// </param>
        /// <returns>
        /// The default bitrate for these parameters.
        /// </returns>
        public static int GetDefaultBitrate(HBAudioEncoder encoder, int sampleRate, HBMixdown mixdown)
        {
            return HBFunctions.hb_audio_bitrate_get_default((uint)encoder.Id, sampleRate, mixdown.Id);
        }

        /// <summary>
        /// Gets limits on audio quality for a given encoder.
        /// </summary>
        /// <param name="encoderId">
        /// The audio encoder ID.
        /// </param>
        /// <returns>
        /// Limits on the audio quality for the given encoder.
        /// </returns>
        public static RangeLimits GetAudioQualityLimits(int encoderId)
        {
            float low = 0, high = 0, granularity = 0;
            int direction = 0;
            HBFunctions.hb_audio_quality_get_limits((uint)encoderId, ref low, ref high, ref granularity, ref direction);

            return new RangeLimits(direction == 0, granularity, high, low);
        }

        /// <summary>
        /// Gets limits on audio compression for a given encoder.
        /// </summary>
        /// <param name="encoderId">
        /// The audio encoder ID.
        /// </param>
        /// <returns>
        /// Limits on the audio compression for the given encoder.
        /// </returns>
        public static RangeLimits GetAudioCompressionLimits(int encoderId)
        {
            float low = 0, high = 0, granularity = 0;
            int direction = 0;
            HBFunctions.hb_audio_compression_get_limits((uint)encoderId, ref low, ref high, ref granularity, ref direction);

            return new RangeLimits(direction == 0, granularity, high, low);
        }

        /// <summary>
        /// The get default quality.
        /// </summary>
        /// <param name="encoder">
        /// The encoder.
        /// </param>
        /// <returns>
        /// The <see cref="double"/>.
        /// </returns>
        public static double GetDefaultQuality(HBAudioEncoder encoder)
        {
           return HBFunctions.hb_audio_quality_get_default((uint)encoder.Id);
        }

        /// <summary>
        /// The get default audio compression.
        /// </summary>
        /// <param name="encoder">
        /// The encoder.
        /// </param>
        /// <returns>
        /// The <see cref="double"/>.
        /// </returns>
        public static double GetDefaultAudioCompression(HBAudioEncoder encoder)
        {
            return HBFunctions.hb_audio_compression_get_default((uint)encoder.Id);
        }
    }
}