// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrake.Interop.Model
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using HandBrake.Interop.HbLib;
using HandBrake.Interop.Model.Encoding;
using HandBrake.Interop.SourceData;
public static class Encoders
{
private static List audioEncoders;
private static List videoEncoders;
private static List mixdowns;
private static List audioBitrates;
///
/// Gets a list of supported audio encoders.
///
public static List AudioEncoders
{
get
{
if (audioEncoders == null)
{
IntPtr encodersPtr = HBFunctions.hb_get_audio_encoders();
int encoderCount = HBFunctions.hb_get_audio_encoders_count();
audioEncoders = InteropUtilities.ConvertArray(encodersPtr, encoderCount)
.Select(Converters.NativeToAudioEncoder)
.ToList();
}
return audioEncoders;
}
}
///
/// Gets a list of supported video encoders.
///
public static List VideoEncoders
{
get
{
if (videoEncoders == null)
{
IntPtr encodersPtr = HBFunctions.hb_get_video_encoders();
int encoderCount = HBFunctions.hb_get_video_encoders_count();
videoEncoders = InteropUtilities.ConvertArray(encodersPtr, encoderCount)
.Select(Converters.NativeToVideoEncoder)
.ToList();
}
return videoEncoders;
}
}
///
/// Gets a list of supported mixdowns.
///
public static List Mixdowns
{
get
{
if (mixdowns == null)
{
IntPtr mixdownsPtr = HBFunctions.hb_get_audio_mixdowns();
int mixdownsCount = HBFunctions.hb_get_audio_mixdowns_count();
mixdowns = InteropUtilities.ConvertArray(mixdownsPtr, mixdownsCount)
.Select(Converters.NativeToMixdown)
.ToList();
}
return mixdowns;
}
}
///
/// Gets a list of supported audio bitrates.
///
public static List AudioBitrates
{
get
{
if (audioBitrates == null)
{
IntPtr audioBitratesPtr = HBFunctions.hb_get_audio_bitrates();
int audioBitratesCount = HBFunctions.hb_get_audio_bitrates_count();
audioBitrates = InteropUtilities.ConvertArray(audioBitratesPtr, audioBitratesCount)
.Select(b => b.rate)
.ToList();
}
return audioBitrates;
}
}
///
/// 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 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);
}
///
/// 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;
}
///
/// Finds the highest possible mixdown for a given audio encoder.
///
/// The audio encoder in question.
/// The highest possible mixdown for that audio encoder.
public static int GetMaxMixdownIndex(HBAudioEncoder audioEncoder)
{
// To find best case scenario, pass in highest number of channels and 6 channel discrete mixdown.
int maxMixdownId = HBFunctions.hb_get_best_mixdown((uint)audioEncoder.Id, NativeConstants.HB_INPUT_CH_LAYOUT_3F4R, NativeConstants.HB_AMIXDOWN_6CH);
for (int i = 0; i < Mixdowns.Count; i++)
{
if (Mixdowns[i].Id == maxMixdownId)
{
return i;
}
}
return -1;
}
///
/// 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, int layout)
{
int sanitizedMixdown = HBFunctions.hb_get_best_mixdown((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, int layout)
{
int defaultMixdown = HBFunctions.hb_get_default_mixdown((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_get_audio_bitrate_limits((uint)encoder.Id, sampleRate, mixdown.Id, ref low, ref high);
return new BitrateLimits { Low = low, High = high };
}
///
/// 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_get_best_audio_bitrate((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_get_default_audio_bitrate((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_get_audio_quality_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_get_audio_compression_limits((uint)encoderId, ref low, ref high, ref granularity, ref direction);
return new RangeLimits
{
Low = low,
High = high,
Granularity = granularity,
Ascending = direction == 0
};
}
}
}