// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// The encoders.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrake.Interop.Model
{
using System;
using System.Collections.Generic;
using System.Linq;
using HandBrake.Interop.HbLib;
using HandBrake.Interop.Model.Encoding;
using HandBrake.Interop.SourceData;
///
/// The encoders.
///
public static class Encoders
{
///
/// The audio encoders.
///
private static List audioEncoders;
///
/// The video encoders.
///
private static List videoEncoders;
///
/// Video framerates in pts.
///
private static List videoFramerates;
///
/// List of HandBrake mixdowns.
///
private static List mixdowns;
///
/// List of HandBrake containers.
///
private static List containers;
///
/// The audio bitrates.
///
private static List audioBitrates;
///
/// Audio sample rates in Hz.
///
private static List audioSampleRates;
///
/// Initializes static members of the Encoders class.
///
static Encoders()
{
HandBrakeUtils.EnsureGlobalInit();
}
///
/// Gets a list of supported audio encoders.
///
public static List AudioEncoders
{
get
{
if (audioEncoders == null)
{
audioEncoders = InteropUtilities.GetListFromIterator(HBFunctions.hb_audio_encoder_get_next, Converters.NativeToAudioEncoder);
}
return audioEncoders;
}
}
///
/// Gets a list of supported video encoders.
///
public static List VideoEncoders
{
get
{
if (videoEncoders == null)
{
videoEncoders = InteropUtilities.GetListFromIterator(HBFunctions.hb_video_encoder_get_next, Converters.NativeToVideoEncoder);
}
return videoEncoders;
}
}
///
/// Gets a list of supported video framerates (in pts).
///
public static List VideoFramerates
{
get
{
if (videoFramerates == null)
{
videoFramerates = InteropUtilities.GetListFromIterator(HBFunctions.hb_video_framerate_get_next, Converters.NativeToRate);
}
return videoFramerates;
}
}
///
/// Gets a list of supported mixdowns.
///
public static List Mixdowns
{
get
{
if (mixdowns == null)
{
mixdowns = InteropUtilities.GetListFromIterator(HBFunctions.hb_mixdown_get_next, Converters.NativeToMixdown);
}
return mixdowns;
}
}
///
/// Gets a list of supported audio bitrates.
///
public static List AudioBitrates
{
get
{
if (audioBitrates == null)
{
audioBitrates = InteropUtilities.GetListFromIterator(HBFunctions.hb_audio_bitrate_get_next, b => b.rate);
}
return audioBitrates;
}
}
///
/// Gets a list of supported audio sample rates (in Hz).
///
public static List AudioSampleRates
{
get
{
if (audioSampleRates == null)
{
audioSampleRates = InteropUtilities.GetListFromIterator(HBFunctions.hb_audio_samplerate_get_next, Converters.NativeToRate);
}
return audioSampleRates;
}
}
///
/// Gets a list of supported containers.
///
public static List Containers
{
get
{
if (containers == null)
{
containers = InteropUtilities.GetListFromIterator(HBFunctions.hb_container_get_next, Converters.NativeToContainer);
}
return containers;
}
}
///
/// Gets the audio encoder with the specified short name.
///
/// The name of the audio encoder.
/// The requested audio encoder.
public static HBAudioEncoder GetAudioEncoder(string shortName)
{
return AudioEncoders.SingleOrDefault(e => e.ShortName == shortName);
}
///
/// Gets the video encoder with the specified short name.
///
/// The name of the video encoder.
/// The requested video encoder.
public static HBVideoEncoder GetVideoEncoder(string shortName)
{
return VideoEncoders.SingleOrDefault(e => e.ShortName == shortName);
}
///
/// Gets the mixdown with the specified short name.
///
/// The name of the mixdown.
/// The requested mixdown.
public static HBMixdown GetMixdown(string shortName)
{
return Mixdowns.SingleOrDefault(m => m.ShortName == shortName);
}
///
/// Gets the container with the specified short name.
///
/// The name of the container.
/// The requested container.
public static HBContainer GetContainer(string shortName)
{
return Containers.SingleOrDefault(c => c.ShortName == shortName);
}
///
/// Determines if the given encoder is compatible with the given track.
///
/// The audio track to examine.
/// The encoder to examine.
/// True if the given encoder is comatible with the given audio track.
/// Only works with passthrough encoders.
public static bool AudioEncoderIsCompatible(AudioTrack track, HBAudioEncoder encoder)
{
return (track.CodecId & encoder.Id) > 0;
}
///
/// Determines if the given mixdown supports the given channel layout.
///
/// The mixdown to evaluate.
/// The channel layout to evaluate.
/// True if the mixdown supports the given channel layout.
public static bool MixdownHasRemixSupport(HBMixdown mixdown, ulong layout)
{
return HBFunctions.hb_mixdown_has_remix_support(mixdown.Id, layout) > 0;
}
///
/// Determines if the given encoder supports the given mixdown.
///
/// The mixdown to evaluate.
/// The encoder to evaluate.
/// True if the encoder supports the mixdown.
public static bool MixdownHasCodecSupport(HBMixdown mixdown, HBAudioEncoder encoder)
{
return HBFunctions.hb_mixdown_has_codec_support(mixdown.Id, (uint) encoder.Id) > 0;
}
///
/// Sanitizes a mixdown given the output codec and input channel layout.
///
/// The desired mixdown.
/// The output encoder to be used.
/// The input channel layout.
/// A sanitized mixdown value.
public static HBMixdown SanitizeMixdown(HBMixdown mixdown, HBAudioEncoder encoder, ulong layout)
{
int sanitizedMixdown = HBFunctions.hb_mixdown_get_best((uint)encoder.Id, layout, mixdown.Id);
return Mixdowns.Single(m => m.Id == sanitizedMixdown);
}
///
/// Gets the default mixdown for the given audio encoder and channel layout.
///
/// The output codec to be used.
/// The input channel layout.
/// The default mixdown for the given codec and channel layout.
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);
}
///
/// Gets the bitrate limits for the given audio codec, sample rate and mixdown.
///
/// The audio encoder used.
/// The sample rate used (Hz).
/// The mixdown used.
/// Limits on the audio bitrate for the given settings.
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 = low, High = high };
}
///
/// Gets the video quality limits for the given video codec.
///
/// The video encoder to check.
/// Limits on the video quality for the encoder.
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 = low,
High = high,
Granularity = granularity,
Ascending = direction == 0
};
}
///
/// Sanitizes an audio bitrate given the output codec, sample rate and mixdown.
///
/// The desired audio bitrate.
/// The output encoder to be used.
/// The output sample rate to be used.
/// The mixdown to be used.
/// A sanitized audio bitrate.
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);
}
///
/// Gets the default audio bitrate for the given parameters.
///
/// The encoder to use.
/// The sample rate to use.
/// The mixdown to use.
/// The default bitrate for these parameters.
public static int GetDefaultBitrate(HBAudioEncoder encoder, int sampleRate, HBMixdown mixdown)
{
return HBFunctions.hb_audio_bitrate_get_default((uint) encoder.Id, sampleRate, mixdown.Id);
}
///
/// Gets limits on audio quality for a given encoder.
///
/// The audio encoder ID.
/// Limits on the audio quality for the given encoder.
internal 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
{
Low = low,
High = high,
Granularity = granularity,
Ascending = direction == 0
};
}
///
/// Gets limits on audio compression for a given encoder.
///
/// The audio encoder ID.
/// Limits on the audio compression for the given encoder.
internal 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
{
Low = low,
High = high,
Granularity = granularity,
Ascending = direction == 0
};
}
}
}