| | 1 | | #nullable disable |
| | 2 | |
|
| | 3 | | #pragma warning disable CS1591 |
| | 4 | | // We need lowercase normalized string for ffmpeg |
| | 5 | | #pragma warning disable CA1308 |
| | 6 | |
|
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Globalization; |
| | 10 | | using System.IO; |
| | 11 | | using System.Linq; |
| | 12 | | using System.Runtime.InteropServices; |
| | 13 | | using System.Text; |
| | 14 | | using System.Text.RegularExpressions; |
| | 15 | | using System.Threading; |
| | 16 | | using Jellyfin.Data; |
| | 17 | | using Jellyfin.Data.Enums; |
| | 18 | | using Jellyfin.Database.Implementations.Enums; |
| | 19 | | using Jellyfin.Extensions; |
| | 20 | | using MediaBrowser.Common.Configuration; |
| | 21 | | using MediaBrowser.Controller.Extensions; |
| | 22 | | using MediaBrowser.Model.Configuration; |
| | 23 | | using MediaBrowser.Model.Dlna; |
| | 24 | | using MediaBrowser.Model.Dto; |
| | 25 | | using MediaBrowser.Model.Entities; |
| | 26 | | using MediaBrowser.Model.MediaInfo; |
| | 27 | | using Microsoft.Extensions.Configuration; |
| | 28 | | using IConfigurationManager = MediaBrowser.Common.Configuration.IConfigurationManager; |
| | 29 | |
|
| | 30 | | namespace MediaBrowser.Controller.MediaEncoding |
| | 31 | | { |
| | 32 | | public partial class EncodingHelper |
| | 33 | | { |
| | 34 | | /// <summary> |
| | 35 | | /// The codec validation regex. |
| | 36 | | /// This regular expression matches strings that consist of alphanumeric characters, hyphens, |
| | 37 | | /// periods, underscores, commas, and vertical bars, with a length between 0 and 40 characters. |
| | 38 | | /// This should matches all common valid codecs. |
| | 39 | | /// </summary> |
| | 40 | | public const string ValidationRegex = @"^[a-zA-Z0-9\-\._,|]{0,40}$"; |
| | 41 | |
|
| | 42 | | private const string _defaultMjpegEncoder = "mjpeg"; |
| | 43 | |
|
| | 44 | | private const string QsvAlias = "qs"; |
| | 45 | | private const string VaapiAlias = "va"; |
| | 46 | | private const string D3d11vaAlias = "dx11"; |
| | 47 | | private const string VideotoolboxAlias = "vt"; |
| | 48 | | private const string RkmppAlias = "rk"; |
| | 49 | | private const string OpenclAlias = "ocl"; |
| | 50 | | private const string CudaAlias = "cu"; |
| | 51 | | private const string DrmAlias = "dr"; |
| | 52 | | private const string VulkanAlias = "vk"; |
| | 53 | | private readonly IApplicationPaths _appPaths; |
| | 54 | | private readonly IMediaEncoder _mediaEncoder; |
| | 55 | | private readonly ISubtitleEncoder _subtitleEncoder; |
| | 56 | | private readonly IConfiguration _config; |
| | 57 | | private readonly IConfigurationManager _configurationManager; |
| | 58 | |
|
| | 59 | | // i915 hang was fixed by linux 6.2 (3f882f2) |
| 21 | 60 | | private readonly Version _minKerneli915Hang = new Version(5, 18); |
| 21 | 61 | | private readonly Version _maxKerneli915Hang = new Version(6, 1, 3); |
| 21 | 62 | | private readonly Version _minFixedKernel60i915Hang = new Version(6, 0, 18); |
| 21 | 63 | | private readonly Version _minKernelVersionAmdVkFmtModifier = new Version(5, 15); |
| | 64 | |
|
| 21 | 65 | | private readonly Version _minFFmpegImplicitHwaccel = new Version(6, 0); |
| 21 | 66 | | private readonly Version _minFFmpegHwaUnsafeOutput = new Version(6, 0); |
| 21 | 67 | | private readonly Version _minFFmpegOclCuTonemapMode = new Version(5, 1, 3); |
| 21 | 68 | | private readonly Version _minFFmpegSvtAv1Params = new Version(5, 1); |
| 21 | 69 | | private readonly Version _minFFmpegVaapiH26xEncA53CcSei = new Version(6, 0); |
| 21 | 70 | | private readonly Version _minFFmpegReadrateOption = new Version(5, 0); |
| 21 | 71 | | private readonly Version _minFFmpegWorkingVtHwSurface = new Version(7, 0, 1); |
| 21 | 72 | | private readonly Version _minFFmpegDisplayRotationOption = new Version(6, 0); |
| 21 | 73 | | private readonly Version _minFFmpegAdvancedTonemapMode = new Version(7, 0, 1); |
| 21 | 74 | | private readonly Version _minFFmpegAlteredVaVkInterop = new Version(7, 0, 1); |
| 21 | 75 | | private readonly Version _minFFmpegQsvVppTonemapOption = new Version(7, 0, 1); |
| 21 | 76 | | private readonly Version _minFFmpegQsvVppOutRangeOption = new Version(7, 0, 1); |
| 21 | 77 | | private readonly Version _minFFmpegVaapiDeviceVendorId = new Version(7, 0, 1); |
| 21 | 78 | | private readonly Version _minFFmpegQsvVppScaleModeOption = new Version(6, 0); |
| | 79 | |
|
| 0 | 80 | | private static readonly Regex _validationRegex = new(ValidationRegex, RegexOptions.Compiled); |
| | 81 | |
|
| 0 | 82 | | private static readonly string[] _videoProfilesH264 = |
| 0 | 83 | | [ |
| 0 | 84 | | "ConstrainedBaseline", |
| 0 | 85 | | "Baseline", |
| 0 | 86 | | "Extended", |
| 0 | 87 | | "Main", |
| 0 | 88 | | "High", |
| 0 | 89 | | "ProgressiveHigh", |
| 0 | 90 | | "ConstrainedHigh", |
| 0 | 91 | | "High10" |
| 0 | 92 | | ]; |
| | 93 | |
|
| 0 | 94 | | private static readonly string[] _videoProfilesH265 = |
| 0 | 95 | | [ |
| 0 | 96 | | "Main", |
| 0 | 97 | | "Main10" |
| 0 | 98 | | ]; |
| | 99 | |
|
| 0 | 100 | | private static readonly string[] _videoProfilesAv1 = |
| 0 | 101 | | [ |
| 0 | 102 | | "Main", |
| 0 | 103 | | "High", |
| 0 | 104 | | "Professional", |
| 0 | 105 | | ]; |
| | 106 | |
|
| 0 | 107 | | private static readonly HashSet<string> _mp4ContainerNames = new(StringComparer.OrdinalIgnoreCase) |
| 0 | 108 | | { |
| 0 | 109 | | "mp4", |
| 0 | 110 | | "m4a", |
| 0 | 111 | | "m4p", |
| 0 | 112 | | "m4b", |
| 0 | 113 | | "m4r", |
| 0 | 114 | | "m4v", |
| 0 | 115 | | }; |
| | 116 | |
|
| 0 | 117 | | private static readonly TonemappingMode[] _legacyTonemapModes = [TonemappingMode.max, TonemappingMode.rgb]; |
| 0 | 118 | | private static readonly TonemappingMode[] _advancedTonemapModes = [TonemappingMode.lum, TonemappingMode.itp]; |
| | 119 | |
|
| | 120 | | // Set max transcoding channels for encoders that can't handle more than a set amount of channels |
| | 121 | | // AAC, FLAC, ALAC, libopus, libvorbis encoders all support at least 8 channels |
| 0 | 122 | | private static readonly Dictionary<string, int> _audioTranscodeChannelLookup = new(StringComparer.OrdinalIgnoreC |
| 0 | 123 | | { |
| 0 | 124 | | { "libmp3lame", 2 }, |
| 0 | 125 | | { "libfdk_aac", 6 }, |
| 0 | 126 | | { "ac3", 6 }, |
| 0 | 127 | | { "eac3", 6 }, |
| 0 | 128 | | { "dca", 6 }, |
| 0 | 129 | | { "mlp", 6 }, |
| 0 | 130 | | { "truehd", 6 }, |
| 0 | 131 | | }; |
| | 132 | |
|
| 0 | 133 | | private static readonly Dictionary<HardwareAccelerationType, string> _mjpegCodecMap = new() |
| 0 | 134 | | { |
| 0 | 135 | | { HardwareAccelerationType.vaapi, _defaultMjpegEncoder + "_vaapi" }, |
| 0 | 136 | | { HardwareAccelerationType.qsv, _defaultMjpegEncoder + "_qsv" }, |
| 0 | 137 | | { HardwareAccelerationType.videotoolbox, _defaultMjpegEncoder + "_videotoolbox" }, |
| 0 | 138 | | { HardwareAccelerationType.rkmpp, _defaultMjpegEncoder + "_rkmpp" } |
| 0 | 139 | | }; |
| | 140 | |
|
| 0 | 141 | | public static readonly string[] LosslessAudioCodecs = |
| 0 | 142 | | [ |
| 0 | 143 | | "alac", |
| 0 | 144 | | "ape", |
| 0 | 145 | | "flac", |
| 0 | 146 | | "mlp", |
| 0 | 147 | | "truehd", |
| 0 | 148 | | "wavpack" |
| 0 | 149 | | ]; |
| | 150 | |
|
| | 151 | | public EncodingHelper( |
| | 152 | | IApplicationPaths appPaths, |
| | 153 | | IMediaEncoder mediaEncoder, |
| | 154 | | ISubtitleEncoder subtitleEncoder, |
| | 155 | | IConfiguration config, |
| | 156 | | IConfigurationManager configurationManager) |
| | 157 | | { |
| 21 | 158 | | _appPaths = appPaths; |
| 21 | 159 | | _mediaEncoder = mediaEncoder; |
| 21 | 160 | | _subtitleEncoder = subtitleEncoder; |
| 21 | 161 | | _config = config; |
| 21 | 162 | | _configurationManager = configurationManager; |
| 21 | 163 | | } |
| | 164 | |
|
| | 165 | | [GeneratedRegex(@"\s+")] |
| | 166 | | private static partial Regex WhiteSpaceRegex(); |
| | 167 | |
|
| | 168 | | public string GetH264Encoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| 0 | 169 | | => GetH26xOrAv1Encoder("libx264", "h264", state, encodingOptions); |
| | 170 | |
|
| | 171 | | public string GetH265Encoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| 0 | 172 | | => GetH26xOrAv1Encoder("libx265", "hevc", state, encodingOptions); |
| | 173 | |
|
| | 174 | | public string GetAv1Encoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| 0 | 175 | | => GetH26xOrAv1Encoder("libsvtav1", "av1", state, encodingOptions); |
| | 176 | |
|
| | 177 | | private string GetH26xOrAv1Encoder(string defaultEncoder, string hwEncoder, EncodingJobInfo state, EncodingOptio |
| | 178 | | { |
| | 179 | | // Only use alternative encoders for video files. |
| | 180 | | // When using concat with folder rips, if the mfx session fails to initialize, ffmpeg will be stuck retrying |
| | 181 | | // Since transcoding of folder rips is experimental anyway, it's not worth adding additional variables such |
| 0 | 182 | | if (state.VideoType == VideoType.VideoFile) |
| | 183 | | { |
| 0 | 184 | | var hwType = encodingOptions.HardwareAccelerationType; |
| | 185 | |
|
| 0 | 186 | | var codecMap = new Dictionary<HardwareAccelerationType, string>() |
| 0 | 187 | | { |
| 0 | 188 | | { HardwareAccelerationType.amf, hwEncoder + "_amf" }, |
| 0 | 189 | | { HardwareAccelerationType.nvenc, hwEncoder + "_nvenc" }, |
| 0 | 190 | | { HardwareAccelerationType.qsv, hwEncoder + "_qsv" }, |
| 0 | 191 | | { HardwareAccelerationType.vaapi, hwEncoder + "_vaapi" }, |
| 0 | 192 | | { HardwareAccelerationType.videotoolbox, hwEncoder + "_videotoolbox" }, |
| 0 | 193 | | { HardwareAccelerationType.v4l2m2m, hwEncoder + "_v4l2m2m" }, |
| 0 | 194 | | { HardwareAccelerationType.rkmpp, hwEncoder + "_rkmpp" }, |
| 0 | 195 | | }; |
| | 196 | |
|
| 0 | 197 | | if (hwType != HardwareAccelerationType.none |
| 0 | 198 | | && encodingOptions.EnableHardwareEncoding |
| 0 | 199 | | && codecMap.TryGetValue(hwType, out var preferredEncoder) |
| 0 | 200 | | && _mediaEncoder.SupportsEncoder(preferredEncoder)) |
| | 201 | | { |
| 0 | 202 | | return preferredEncoder; |
| | 203 | | } |
| | 204 | | } |
| | 205 | |
|
| 0 | 206 | | return defaultEncoder; |
| | 207 | | } |
| | 208 | |
|
| | 209 | | private string GetMjpegEncoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| | 210 | | { |
| 0 | 211 | | if (state.VideoType == VideoType.VideoFile) |
| | 212 | | { |
| 0 | 213 | | var hwType = encodingOptions.HardwareAccelerationType; |
| | 214 | |
|
| | 215 | | // Only Intel has VA-API MJPEG encoder |
| 0 | 216 | | if (hwType == HardwareAccelerationType.vaapi |
| 0 | 217 | | && !(_mediaEncoder.IsVaapiDeviceInteliHD |
| 0 | 218 | | || _mediaEncoder.IsVaapiDeviceInteli965)) |
| | 219 | | { |
| 0 | 220 | | return _defaultMjpegEncoder; |
| | 221 | | } |
| | 222 | |
|
| 0 | 223 | | if (hwType != HardwareAccelerationType.none |
| 0 | 224 | | && encodingOptions.EnableHardwareEncoding |
| 0 | 225 | | && _mjpegCodecMap.TryGetValue(hwType, out var preferredEncoder) |
| 0 | 226 | | && _mediaEncoder.SupportsEncoder(preferredEncoder)) |
| | 227 | | { |
| 0 | 228 | | return preferredEncoder; |
| | 229 | | } |
| | 230 | | } |
| | 231 | |
|
| 0 | 232 | | return _defaultMjpegEncoder; |
| | 233 | | } |
| | 234 | |
|
| | 235 | | private bool IsVaapiSupported(EncodingJobInfo state) |
| | 236 | | { |
| | 237 | | // vaapi will throw an error with this input |
| | 238 | | // [vaapi @ 0x7faed8000960] No VAAPI support for codec mpeg4 profile -99. |
| 0 | 239 | | if (string.Equals(state.VideoStream?.Codec, "mpeg4", StringComparison.OrdinalIgnoreCase)) |
| | 240 | | { |
| 0 | 241 | | return false; |
| | 242 | | } |
| | 243 | |
|
| 0 | 244 | | return _mediaEncoder.SupportsHwaccel("vaapi"); |
| | 245 | | } |
| | 246 | |
|
| | 247 | | private bool IsVaapiFullSupported() |
| | 248 | | { |
| 0 | 249 | | return _mediaEncoder.SupportsHwaccel("drm") |
| 0 | 250 | | && _mediaEncoder.SupportsHwaccel("vaapi") |
| 0 | 251 | | && _mediaEncoder.SupportsFilter("scale_vaapi") |
| 0 | 252 | | && _mediaEncoder.SupportsFilter("deinterlace_vaapi") |
| 0 | 253 | | && _mediaEncoder.SupportsFilter("tonemap_vaapi") |
| 0 | 254 | | && _mediaEncoder.SupportsFilter("procamp_vaapi") |
| 0 | 255 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.OverlayVaapiFrameSync) |
| 0 | 256 | | && _mediaEncoder.SupportsFilter("transpose_vaapi") |
| 0 | 257 | | && _mediaEncoder.SupportsFilter("hwupload_vaapi"); |
| | 258 | | } |
| | 259 | |
|
| | 260 | | private bool IsRkmppFullSupported() |
| | 261 | | { |
| 0 | 262 | | return _mediaEncoder.SupportsHwaccel("rkmpp") |
| 0 | 263 | | && _mediaEncoder.SupportsFilter("scale_rkrga") |
| 0 | 264 | | && _mediaEncoder.SupportsFilter("vpp_rkrga") |
| 0 | 265 | | && _mediaEncoder.SupportsFilter("overlay_rkrga"); |
| | 266 | | } |
| | 267 | |
|
| | 268 | | private bool IsOpenclFullSupported() |
| | 269 | | { |
| 0 | 270 | | return _mediaEncoder.SupportsHwaccel("opencl") |
| 0 | 271 | | && _mediaEncoder.SupportsFilter("scale_opencl") |
| 0 | 272 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.TonemapOpenclBt2390) |
| 0 | 273 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.OverlayOpenclFrameSync); |
| | 274 | |
|
| | 275 | | // Let transpose_opencl optional for the time being. |
| | 276 | | } |
| | 277 | |
|
| | 278 | | private bool IsCudaFullSupported() |
| | 279 | | { |
| 0 | 280 | | return _mediaEncoder.SupportsHwaccel("cuda") |
| 0 | 281 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.ScaleCudaFormat) |
| 0 | 282 | | && _mediaEncoder.SupportsFilter("yadif_cuda") |
| 0 | 283 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.TonemapCudaName) |
| 0 | 284 | | && _mediaEncoder.SupportsFilter("overlay_cuda") |
| 0 | 285 | | && _mediaEncoder.SupportsFilter("hwupload_cuda"); |
| | 286 | |
|
| | 287 | | // Let transpose_cuda optional for the time being. |
| | 288 | | } |
| | 289 | |
|
| | 290 | | private bool IsVulkanFullSupported() |
| | 291 | | { |
| 0 | 292 | | return _mediaEncoder.SupportsHwaccel("vulkan") |
| 0 | 293 | | && _mediaEncoder.SupportsFilter("libplacebo") |
| 0 | 294 | | && _mediaEncoder.SupportsFilter("scale_vulkan") |
| 0 | 295 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.OverlayVulkanFrameSync) |
| 0 | 296 | | && _mediaEncoder.SupportsFilter("transpose_vulkan") |
| 0 | 297 | | && _mediaEncoder.SupportsFilter("flip_vulkan"); |
| | 298 | | } |
| | 299 | |
|
| | 300 | | private bool IsVideoToolboxFullSupported() |
| | 301 | | { |
| 0 | 302 | | return _mediaEncoder.SupportsHwaccel("videotoolbox") |
| 0 | 303 | | && _mediaEncoder.SupportsFilter("yadif_videotoolbox") |
| 0 | 304 | | && _mediaEncoder.SupportsFilter("overlay_videotoolbox") |
| 0 | 305 | | && _mediaEncoder.SupportsFilter("tonemap_videotoolbox") |
| 0 | 306 | | && _mediaEncoder.SupportsFilter("scale_vt"); |
| | 307 | |
|
| | 308 | | // Let transpose_vt optional for the time being. |
| | 309 | | } |
| | 310 | |
|
| | 311 | | private bool IsSwTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 312 | | { |
| 0 | 313 | | if (state.VideoStream is null |
| 0 | 314 | | || GetVideoColorBitDepth(state) < 10 |
| 0 | 315 | | || !_mediaEncoder.SupportsFilter("tonemapx")) |
| | 316 | | { |
| 0 | 317 | | return false; |
| | 318 | | } |
| | 319 | |
|
| 0 | 320 | | return state.VideoStream.VideoRange == VideoRange.HDR; |
| | 321 | | } |
| | 322 | |
|
| | 323 | | private bool IsHwTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 324 | | { |
| 0 | 325 | | if (state.VideoStream is null |
| 0 | 326 | | || !options.EnableTonemapping |
| 0 | 327 | | || GetVideoColorBitDepth(state) < 10) |
| | 328 | | { |
| 0 | 329 | | return false; |
| | 330 | | } |
| | 331 | |
|
| 0 | 332 | | if (state.VideoStream.VideoRange == VideoRange.HDR |
| 0 | 333 | | && state.VideoStream.VideoRangeType == VideoRangeType.DOVI) |
| | 334 | | { |
| | 335 | | // Only native SW decoder and HW accelerator can parse dovi rpu. |
| 0 | 336 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 337 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 338 | | var isNvdecDecoder = vidDecoder.Contains("cuda", StringComparison.OrdinalIgnoreCase); |
| 0 | 339 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 340 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 341 | | var isVideoToolBoxDecoder = vidDecoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 342 | | return isSwDecoder || isNvdecDecoder || isVaapiDecoder || isD3d11vaDecoder || isVideoToolBoxDecoder; |
| | 343 | | } |
| | 344 | |
|
| 0 | 345 | | return state.VideoStream.VideoRange == VideoRange.HDR |
| 0 | 346 | | && (state.VideoStream.VideoRangeType == VideoRangeType.HDR10 |
| 0 | 347 | | || state.VideoStream.VideoRangeType == VideoRangeType.HLG |
| 0 | 348 | | || state.VideoStream.VideoRangeType == VideoRangeType.DOVIWithHDR10 |
| 0 | 349 | | || state.VideoStream.VideoRangeType == VideoRangeType.DOVIWithHLG); |
| | 350 | | } |
| | 351 | |
|
| | 352 | | private bool IsVulkanHwTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 353 | | { |
| 0 | 354 | | if (state.VideoStream is null) |
| | 355 | | { |
| 0 | 356 | | return false; |
| | 357 | | } |
| | 358 | |
|
| | 359 | | // libplacebo has partial Dolby Vision to SDR tonemapping support. |
| 0 | 360 | | return options.EnableTonemapping |
| 0 | 361 | | && state.VideoStream.VideoRange == VideoRange.HDR |
| 0 | 362 | | && GetVideoColorBitDepth(state) == 10; |
| | 363 | | } |
| | 364 | |
|
| | 365 | | private bool IsIntelVppTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 366 | | { |
| 0 | 367 | | if (state.VideoStream is null |
| 0 | 368 | | || !options.EnableVppTonemapping |
| 0 | 369 | | || GetVideoColorBitDepth(state) < 10) |
| | 370 | | { |
| 0 | 371 | | return false; |
| | 372 | | } |
| | 373 | |
|
| | 374 | | // prefer 'tonemap_vaapi' over 'vpp_qsv' on Linux for supporting Gen9/KBLx. |
| | 375 | | // 'vpp_qsv' requires VPL, which is only supported on Gen12/TGLx and newer. |
| 0 | 376 | | if (OperatingSystem.IsWindows() |
| 0 | 377 | | && options.HardwareAccelerationType == HardwareAccelerationType.qsv |
| 0 | 378 | | && _mediaEncoder.EncoderVersion < _minFFmpegQsvVppTonemapOption) |
| | 379 | | { |
| 0 | 380 | | return false; |
| | 381 | | } |
| | 382 | |
|
| 0 | 383 | | return state.VideoStream.VideoRange == VideoRange.HDR |
| 0 | 384 | | && (state.VideoStream.VideoRangeType == VideoRangeType.HDR10 |
| 0 | 385 | | || state.VideoStream.VideoRangeType == VideoRangeType.DOVIWithHDR10); |
| | 386 | | } |
| | 387 | |
|
| | 388 | | private bool IsVideoToolboxTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 389 | | { |
| 0 | 390 | | if (state.VideoStream is null |
| 0 | 391 | | || !options.EnableVideoToolboxTonemapping |
| 0 | 392 | | || GetVideoColorBitDepth(state) < 10) |
| | 393 | | { |
| 0 | 394 | | return false; |
| | 395 | | } |
| | 396 | |
|
| | 397 | | // Certain DV profile 5 video works in Safari with direct playing, but the VideoToolBox does not produce cor |
| | 398 | | // All other HDR formats working. |
| 0 | 399 | | return state.VideoStream.VideoRange == VideoRange.HDR |
| 0 | 400 | | && state.VideoStream.VideoRangeType is VideoRangeType.HDR10 or VideoRangeType.HLG or VideoRangeType.H |
| | 401 | | } |
| | 402 | |
|
| | 403 | | private bool IsVideoStreamHevcRext(EncodingJobInfo state) |
| | 404 | | { |
| 0 | 405 | | var videoStream = state.VideoStream; |
| 0 | 406 | | if (videoStream is null) |
| | 407 | | { |
| 0 | 408 | | return false; |
| | 409 | | } |
| | 410 | |
|
| 0 | 411 | | return string.Equals(videoStream.Codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 412 | | && (string.Equals(videoStream.Profile, "Rext", StringComparison.OrdinalIgnoreCase) |
| 0 | 413 | | || string.Equals(videoStream.PixelFormat, "yuv420p12le", StringComparison.OrdinalIgnoreCase) |
| 0 | 414 | | || string.Equals(videoStream.PixelFormat, "yuv422p", StringComparison.OrdinalIgnoreCase) |
| 0 | 415 | | || string.Equals(videoStream.PixelFormat, "yuv422p10le", StringComparison.OrdinalIgnoreCase) |
| 0 | 416 | | || string.Equals(videoStream.PixelFormat, "yuv422p12le", StringComparison.OrdinalIgnoreCase) |
| 0 | 417 | | || string.Equals(videoStream.PixelFormat, "yuv444p", StringComparison.OrdinalIgnoreCase) |
| 0 | 418 | | || string.Equals(videoStream.PixelFormat, "yuv444p10le", StringComparison.OrdinalIgnoreCase) |
| 0 | 419 | | || string.Equals(videoStream.PixelFormat, "yuv444p12le", StringComparison.OrdinalIgnoreCase)); |
| | 420 | | } |
| | 421 | |
|
| | 422 | | /// <summary> |
| | 423 | | /// Gets the name of the output video codec. |
| | 424 | | /// </summary> |
| | 425 | | /// <param name="state">Encoding state.</param> |
| | 426 | | /// <param name="encodingOptions">Encoding options.</param> |
| | 427 | | /// <returns>Encoder string.</returns> |
| | 428 | | public string GetVideoEncoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| | 429 | | { |
| 0 | 430 | | var codec = state.OutputVideoCodec; |
| | 431 | |
|
| 0 | 432 | | if (!string.IsNullOrEmpty(codec)) |
| | 433 | | { |
| 0 | 434 | | if (string.Equals(codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 435 | | { |
| 0 | 436 | | return GetAv1Encoder(state, encodingOptions); |
| | 437 | | } |
| | 438 | |
|
| 0 | 439 | | if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase) |
| 0 | 440 | | || string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | 441 | | { |
| 0 | 442 | | return GetH265Encoder(state, encodingOptions); |
| | 443 | | } |
| | 444 | |
|
| 0 | 445 | | if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 446 | | { |
| 0 | 447 | | return GetH264Encoder(state, encodingOptions); |
| | 448 | | } |
| | 449 | |
|
| 0 | 450 | | if (string.Equals(codec, "mjpeg", StringComparison.OrdinalIgnoreCase)) |
| | 451 | | { |
| 0 | 452 | | return GetMjpegEncoder(state, encodingOptions); |
| | 453 | | } |
| | 454 | |
|
| 0 | 455 | | if (_validationRegex.IsMatch(codec)) |
| | 456 | | { |
| 0 | 457 | | return codec.ToLowerInvariant(); |
| | 458 | | } |
| | 459 | | } |
| | 460 | |
|
| 0 | 461 | | return "copy"; |
| | 462 | | } |
| | 463 | |
|
| | 464 | | /// <summary> |
| | 465 | | /// Gets the user agent param. |
| | 466 | | /// </summary> |
| | 467 | | /// <param name="state">The state.</param> |
| | 468 | | /// <returns>System.String.</returns> |
| | 469 | | public string GetUserAgentParam(EncodingJobInfo state) |
| | 470 | | { |
| 0 | 471 | | if (state.RemoteHttpHeaders.TryGetValue("User-Agent", out string useragent)) |
| | 472 | | { |
| 0 | 473 | | return "-user_agent \"" + useragent + "\""; |
| | 474 | | } |
| | 475 | |
|
| 0 | 476 | | return string.Empty; |
| | 477 | | } |
| | 478 | |
|
| | 479 | | /// <summary> |
| | 480 | | /// Gets the referer param. |
| | 481 | | /// </summary> |
| | 482 | | /// <param name="state">The state.</param> |
| | 483 | | /// <returns>System.String.</returns> |
| | 484 | | public string GetRefererParam(EncodingJobInfo state) |
| | 485 | | { |
| 0 | 486 | | if (state.RemoteHttpHeaders.TryGetValue("Referer", out string referer)) |
| | 487 | | { |
| 0 | 488 | | return "-referer \"" + referer + "\""; |
| | 489 | | } |
| | 490 | |
|
| 0 | 491 | | return string.Empty; |
| | 492 | | } |
| | 493 | |
|
| | 494 | | public static string GetInputFormat(string container) |
| | 495 | | { |
| 0 | 496 | | if (string.IsNullOrEmpty(container) || !_validationRegex.IsMatch(container)) |
| | 497 | | { |
| 0 | 498 | | return null; |
| | 499 | | } |
| | 500 | |
|
| 0 | 501 | | container = container.Replace("mkv", "matroska", StringComparison.OrdinalIgnoreCase); |
| | 502 | |
|
| 0 | 503 | | if (string.Equals(container, "ts", StringComparison.OrdinalIgnoreCase)) |
| | 504 | | { |
| 0 | 505 | | return "mpegts"; |
| | 506 | | } |
| | 507 | |
|
| | 508 | | // For these need to find out the ffmpeg names |
| 0 | 509 | | if (string.Equals(container, "m2ts", StringComparison.OrdinalIgnoreCase)) |
| | 510 | | { |
| 0 | 511 | | return null; |
| | 512 | | } |
| | 513 | |
|
| 0 | 514 | | if (string.Equals(container, "wmv", StringComparison.OrdinalIgnoreCase)) |
| | 515 | | { |
| 0 | 516 | | return null; |
| | 517 | | } |
| | 518 | |
|
| 0 | 519 | | if (string.Equals(container, "mts", StringComparison.OrdinalIgnoreCase)) |
| | 520 | | { |
| 0 | 521 | | return null; |
| | 522 | | } |
| | 523 | |
|
| 0 | 524 | | if (string.Equals(container, "vob", StringComparison.OrdinalIgnoreCase)) |
| | 525 | | { |
| 0 | 526 | | return null; |
| | 527 | | } |
| | 528 | |
|
| 0 | 529 | | if (string.Equals(container, "mpg", StringComparison.OrdinalIgnoreCase)) |
| | 530 | | { |
| 0 | 531 | | return null; |
| | 532 | | } |
| | 533 | |
|
| 0 | 534 | | if (string.Equals(container, "mpeg", StringComparison.OrdinalIgnoreCase)) |
| | 535 | | { |
| 0 | 536 | | return null; |
| | 537 | | } |
| | 538 | |
|
| 0 | 539 | | if (string.Equals(container, "rec", StringComparison.OrdinalIgnoreCase)) |
| | 540 | | { |
| 0 | 541 | | return null; |
| | 542 | | } |
| | 543 | |
|
| 0 | 544 | | if (string.Equals(container, "dvr-ms", StringComparison.OrdinalIgnoreCase)) |
| | 545 | | { |
| 0 | 546 | | return null; |
| | 547 | | } |
| | 548 | |
|
| 0 | 549 | | if (string.Equals(container, "ogm", StringComparison.OrdinalIgnoreCase)) |
| | 550 | | { |
| 0 | 551 | | return null; |
| | 552 | | } |
| | 553 | |
|
| 0 | 554 | | if (string.Equals(container, "divx", StringComparison.OrdinalIgnoreCase)) |
| | 555 | | { |
| 0 | 556 | | return null; |
| | 557 | | } |
| | 558 | |
|
| 0 | 559 | | if (string.Equals(container, "tp", StringComparison.OrdinalIgnoreCase)) |
| | 560 | | { |
| 0 | 561 | | return null; |
| | 562 | | } |
| | 563 | |
|
| 0 | 564 | | if (string.Equals(container, "rmvb", StringComparison.OrdinalIgnoreCase)) |
| | 565 | | { |
| 0 | 566 | | return null; |
| | 567 | | } |
| | 568 | |
|
| 0 | 569 | | if (string.Equals(container, "rtp", StringComparison.OrdinalIgnoreCase)) |
| | 570 | | { |
| 0 | 571 | | return null; |
| | 572 | | } |
| | 573 | |
|
| | 574 | | // Seeing reported failures here, not sure yet if this is related to specifying input format |
| 0 | 575 | | if (string.Equals(container, "m4v", StringComparison.OrdinalIgnoreCase)) |
| | 576 | | { |
| 0 | 577 | | return null; |
| | 578 | | } |
| | 579 | |
|
| | 580 | | // obviously don't do this for strm files |
| 0 | 581 | | if (string.Equals(container, "strm", StringComparison.OrdinalIgnoreCase)) |
| | 582 | | { |
| 0 | 583 | | return null; |
| | 584 | | } |
| | 585 | |
|
| | 586 | | // ISO files don't have an ffmpeg format |
| 0 | 587 | | if (string.Equals(container, "iso", StringComparison.OrdinalIgnoreCase)) |
| | 588 | | { |
| 0 | 589 | | return null; |
| | 590 | | } |
| | 591 | |
|
| 0 | 592 | | return container; |
| | 593 | | } |
| | 594 | |
|
| | 595 | | /// <summary> |
| | 596 | | /// Gets decoder from a codec. |
| | 597 | | /// </summary> |
| | 598 | | /// <param name="codec">Codec to use.</param> |
| | 599 | | /// <returns>Decoder string.</returns> |
| | 600 | | public string GetDecoderFromCodec(string codec) |
| | 601 | | { |
| | 602 | | // For these need to find out the ffmpeg names |
| 0 | 603 | | if (string.Equals(codec, "mp2", StringComparison.OrdinalIgnoreCase)) |
| | 604 | | { |
| 0 | 605 | | return null; |
| | 606 | | } |
| | 607 | |
|
| 0 | 608 | | if (string.Equals(codec, "aac_latm", StringComparison.OrdinalIgnoreCase)) |
| | 609 | | { |
| 0 | 610 | | return null; |
| | 611 | | } |
| | 612 | |
|
| 0 | 613 | | if (string.Equals(codec, "eac3", StringComparison.OrdinalIgnoreCase)) |
| | 614 | | { |
| 0 | 615 | | return null; |
| | 616 | | } |
| | 617 | |
|
| 0 | 618 | | if (_mediaEncoder.SupportsDecoder(codec)) |
| | 619 | | { |
| 0 | 620 | | return codec; |
| | 621 | | } |
| | 622 | |
|
| 0 | 623 | | return null; |
| | 624 | | } |
| | 625 | |
|
| | 626 | | /// <summary> |
| | 627 | | /// Infers the audio codec based on the url. |
| | 628 | | /// </summary> |
| | 629 | | /// <param name="container">Container to use.</param> |
| | 630 | | /// <returns>Codec string.</returns> |
| | 631 | | public string InferAudioCodec(string container) |
| | 632 | | { |
| 0 | 633 | | if (string.IsNullOrWhiteSpace(container)) |
| | 634 | | { |
| | 635 | | // this may not work, but if the client is that broken we cannot do anything better |
| 0 | 636 | | return "aac"; |
| | 637 | | } |
| | 638 | |
|
| 0 | 639 | | var inferredCodec = container.ToLowerInvariant(); |
| | 640 | |
|
| 0 | 641 | | return inferredCodec switch |
| 0 | 642 | | { |
| 0 | 643 | | "ogg" or "oga" or "ogv" or "webm" or "webma" => "opus", |
| 0 | 644 | | "m4a" or "m4b" or "mp4" or "mov" or "mkv" or "mka" => "aac", |
| 0 | 645 | | "ts" or "avi" or "flv" or "f4v" or "swf" => "mp3", |
| 0 | 646 | | _ => inferredCodec |
| 0 | 647 | | }; |
| | 648 | | } |
| | 649 | |
|
| | 650 | | /// <summary> |
| | 651 | | /// Infers the video codec. |
| | 652 | | /// </summary> |
| | 653 | | /// <param name="url">The URL.</param> |
| | 654 | | /// <returns>System.Nullable{VideoCodecs}.</returns> |
| | 655 | | public string InferVideoCodec(string url) |
| | 656 | | { |
| 0 | 657 | | var ext = Path.GetExtension(url.AsSpan()); |
| | 658 | |
|
| 0 | 659 | | if (ext.Equals(".asf", StringComparison.OrdinalIgnoreCase)) |
| | 660 | | { |
| 0 | 661 | | return "wmv"; |
| | 662 | | } |
| | 663 | |
|
| 0 | 664 | | if (ext.Equals(".webm", StringComparison.OrdinalIgnoreCase)) |
| | 665 | | { |
| | 666 | | // TODO: this may not always mean VP8, as the codec ages |
| 0 | 667 | | return "vp8"; |
| | 668 | | } |
| | 669 | |
|
| 0 | 670 | | if (ext.Equals(".ogg", StringComparison.OrdinalIgnoreCase) || ext.Equals(".ogv", StringComparison.OrdinalIgn |
| | 671 | | { |
| 0 | 672 | | return "theora"; |
| | 673 | | } |
| | 674 | |
|
| 0 | 675 | | if (ext.Equals(".m3u8", StringComparison.OrdinalIgnoreCase) || ext.Equals(".ts", StringComparison.OrdinalIgn |
| | 676 | | { |
| 0 | 677 | | return "h264"; |
| | 678 | | } |
| | 679 | |
|
| 0 | 680 | | return "copy"; |
| | 681 | | } |
| | 682 | |
|
| | 683 | | public int GetVideoProfileScore(string videoCodec, string videoProfile) |
| | 684 | | { |
| | 685 | | // strip spaces because they may be stripped out on the query string |
| 0 | 686 | | string profile = videoProfile.Replace(" ", string.Empty, StringComparison.Ordinal); |
| 0 | 687 | | if (string.Equals("h264", videoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 688 | | { |
| 0 | 689 | | return Array.FindIndex(_videoProfilesH264, x => string.Equals(x, profile, StringComparison.OrdinalIgnore |
| | 690 | | } |
| | 691 | |
|
| 0 | 692 | | if (string.Equals("hevc", videoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 693 | | { |
| 0 | 694 | | return Array.FindIndex(_videoProfilesH265, x => string.Equals(x, profile, StringComparison.OrdinalIgnore |
| | 695 | | } |
| | 696 | |
|
| 0 | 697 | | if (string.Equals("av1", videoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 698 | | { |
| 0 | 699 | | return Array.FindIndex(_videoProfilesAv1, x => string.Equals(x, profile, StringComparison.OrdinalIgnoreC |
| | 700 | | } |
| | 701 | |
|
| 0 | 702 | | return -1; |
| | 703 | | } |
| | 704 | |
|
| | 705 | | /// <summary> |
| | 706 | | /// Gets the audio encoder. |
| | 707 | | /// </summary> |
| | 708 | | /// <param name="state">The state.</param> |
| | 709 | | /// <returns>System.String.</returns> |
| | 710 | | public string GetAudioEncoder(EncodingJobInfo state) |
| | 711 | | { |
| 0 | 712 | | var codec = state.OutputAudioCodec; |
| | 713 | |
|
| 0 | 714 | | if (!_validationRegex.IsMatch(codec)) |
| | 715 | | { |
| 0 | 716 | | codec = "aac"; |
| | 717 | | } |
| | 718 | |
|
| 0 | 719 | | if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase)) |
| | 720 | | { |
| | 721 | | // Use Apple's aac encoder if available as it provides best audio quality |
| 0 | 722 | | if (_mediaEncoder.SupportsEncoder("aac_at")) |
| | 723 | | { |
| 0 | 724 | | return "aac_at"; |
| | 725 | | } |
| | 726 | |
|
| | 727 | | // Use libfdk_aac for better audio quality if using custom build of FFmpeg which has fdk_aac support |
| 0 | 728 | | if (_mediaEncoder.SupportsEncoder("libfdk_aac")) |
| | 729 | | { |
| 0 | 730 | | return "libfdk_aac"; |
| | 731 | | } |
| | 732 | |
|
| 0 | 733 | | return "aac"; |
| | 734 | | } |
| | 735 | |
|
| 0 | 736 | | if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase)) |
| | 737 | | { |
| 0 | 738 | | return "libmp3lame"; |
| | 739 | | } |
| | 740 | |
|
| 0 | 741 | | if (string.Equals(codec, "vorbis", StringComparison.OrdinalIgnoreCase)) |
| | 742 | | { |
| 0 | 743 | | return "libvorbis"; |
| | 744 | | } |
| | 745 | |
|
| 0 | 746 | | if (string.Equals(codec, "opus", StringComparison.OrdinalIgnoreCase)) |
| | 747 | | { |
| 0 | 748 | | return "libopus"; |
| | 749 | | } |
| | 750 | |
|
| 0 | 751 | | if (string.Equals(codec, "flac", StringComparison.OrdinalIgnoreCase)) |
| | 752 | | { |
| 0 | 753 | | return "flac"; |
| | 754 | | } |
| | 755 | |
|
| 0 | 756 | | if (string.Equals(codec, "dts", StringComparison.OrdinalIgnoreCase)) |
| | 757 | | { |
| 0 | 758 | | return "dca"; |
| | 759 | | } |
| | 760 | |
|
| 0 | 761 | | if (string.Equals(codec, "alac", StringComparison.OrdinalIgnoreCase)) |
| | 762 | | { |
| | 763 | | // The ffmpeg upstream breaks the AudioToolbox ALAC encoder in version 6.1 but fixes it in version 7.0. |
| | 764 | | // Since ALAC is lossless in quality and the AudioToolbox encoder is not faster, |
| | 765 | | // its only benefit is a smaller file size. |
| | 766 | | // To prevent problems, use the ffmpeg native encoder instead. |
| 0 | 767 | | return "alac"; |
| | 768 | | } |
| | 769 | |
|
| 0 | 770 | | return codec.ToLowerInvariant(); |
| | 771 | | } |
| | 772 | |
|
| | 773 | | private string GetRkmppDeviceArgs(string alias) |
| | 774 | | { |
| 0 | 775 | | alias ??= RkmppAlias; |
| | 776 | |
|
| | 777 | | // device selection in rk is not supported. |
| 0 | 778 | | return " -init_hw_device rkmpp=" + alias; |
| | 779 | | } |
| | 780 | |
|
| | 781 | | private string GetVideoToolboxDeviceArgs(string alias) |
| | 782 | | { |
| 0 | 783 | | alias ??= VideotoolboxAlias; |
| | 784 | |
|
| | 785 | | // device selection in vt is not supported. |
| 0 | 786 | | return " -init_hw_device videotoolbox=" + alias; |
| | 787 | | } |
| | 788 | |
|
| | 789 | | private string GetCudaDeviceArgs(int deviceIndex, string alias) |
| | 790 | | { |
| 0 | 791 | | alias ??= CudaAlias; |
| 0 | 792 | | deviceIndex = deviceIndex >= 0 |
| 0 | 793 | | ? deviceIndex |
| 0 | 794 | | : 0; |
| | 795 | |
|
| 0 | 796 | | return string.Format( |
| 0 | 797 | | CultureInfo.InvariantCulture, |
| 0 | 798 | | " -init_hw_device cuda={0}:{1}", |
| 0 | 799 | | alias, |
| 0 | 800 | | deviceIndex); |
| | 801 | | } |
| | 802 | |
|
| | 803 | | private string GetVulkanDeviceArgs(int deviceIndex, string deviceName, string srcDeviceAlias, string alias) |
| | 804 | | { |
| 0 | 805 | | alias ??= VulkanAlias; |
| 0 | 806 | | deviceIndex = deviceIndex >= 0 |
| 0 | 807 | | ? deviceIndex |
| 0 | 808 | | : 0; |
| 0 | 809 | | var vendorOpts = string.IsNullOrEmpty(deviceName) |
| 0 | 810 | | ? ":" + deviceIndex |
| 0 | 811 | | : ":" + "\"" + deviceName + "\""; |
| 0 | 812 | | var options = string.IsNullOrEmpty(srcDeviceAlias) |
| 0 | 813 | | ? vendorOpts |
| 0 | 814 | | : "@" + srcDeviceAlias; |
| | 815 | |
|
| 0 | 816 | | return string.Format( |
| 0 | 817 | | CultureInfo.InvariantCulture, |
| 0 | 818 | | " -init_hw_device vulkan={0}{1}", |
| 0 | 819 | | alias, |
| 0 | 820 | | options); |
| | 821 | | } |
| | 822 | |
|
| | 823 | | private string GetOpenclDeviceArgs(int deviceIndex, string deviceVendorName, string srcDeviceAlias, string alias |
| | 824 | | { |
| 0 | 825 | | alias ??= OpenclAlias; |
| 0 | 826 | | deviceIndex = deviceIndex >= 0 |
| 0 | 827 | | ? deviceIndex |
| 0 | 828 | | : 0; |
| 0 | 829 | | var vendorOpts = string.IsNullOrEmpty(deviceVendorName) |
| 0 | 830 | | ? ":0.0" |
| 0 | 831 | | : ":." + deviceIndex + ",device_vendor=\"" + deviceVendorName + "\""; |
| 0 | 832 | | var options = string.IsNullOrEmpty(srcDeviceAlias) |
| 0 | 833 | | ? vendorOpts |
| 0 | 834 | | : "@" + srcDeviceAlias; |
| | 835 | |
|
| 0 | 836 | | return string.Format( |
| 0 | 837 | | CultureInfo.InvariantCulture, |
| 0 | 838 | | " -init_hw_device opencl={0}{1}", |
| 0 | 839 | | alias, |
| 0 | 840 | | options); |
| | 841 | | } |
| | 842 | |
|
| | 843 | | private string GetD3d11vaDeviceArgs(int deviceIndex, string deviceVendorId, string alias) |
| | 844 | | { |
| 0 | 845 | | alias ??= D3d11vaAlias; |
| 0 | 846 | | deviceIndex = deviceIndex >= 0 ? deviceIndex : 0; |
| 0 | 847 | | var options = string.IsNullOrEmpty(deviceVendorId) |
| 0 | 848 | | ? deviceIndex.ToString(CultureInfo.InvariantCulture) |
| 0 | 849 | | : ",vendor=" + deviceVendorId; |
| | 850 | |
|
| 0 | 851 | | return string.Format( |
| 0 | 852 | | CultureInfo.InvariantCulture, |
| 0 | 853 | | " -init_hw_device d3d11va={0}:{1}", |
| 0 | 854 | | alias, |
| 0 | 855 | | options); |
| | 856 | | } |
| | 857 | |
|
| | 858 | | private string GetVaapiDeviceArgs(string renderNodePath, string driver, string kernelDriver, string vendorId, st |
| | 859 | | { |
| 0 | 860 | | alias ??= VaapiAlias; |
| 0 | 861 | | var haveVendorId = !string.IsNullOrEmpty(vendorId) |
| 0 | 862 | | && _mediaEncoder.EncoderVersion >= _minFFmpegVaapiDeviceVendorId; |
| | 863 | |
|
| | 864 | | // Priority: 'renderNodePath' > 'vendorId' > 'kernelDriver' |
| 0 | 865 | | var driverOpts = File.Exists(renderNodePath) |
| 0 | 866 | | ? renderNodePath |
| 0 | 867 | | : (haveVendorId ? $",vendor_id={vendorId}" : (string.IsNullOrEmpty(kernelDriver) ? string.Empty : $",ker |
| | 868 | |
|
| | 869 | | // 'driver' behaves similarly to env LIBVA_DRIVER_NAME |
| 0 | 870 | | driverOpts += string.IsNullOrEmpty(driver) ? string.Empty : ",driver=" + driver; |
| | 871 | |
|
| 0 | 872 | | var options = string.IsNullOrEmpty(srcDeviceAlias) |
| 0 | 873 | | ? (string.IsNullOrEmpty(driverOpts) ? string.Empty : ":" + driverOpts) |
| 0 | 874 | | : "@" + srcDeviceAlias; |
| | 875 | |
|
| 0 | 876 | | return string.Format( |
| 0 | 877 | | CultureInfo.InvariantCulture, |
| 0 | 878 | | " -init_hw_device vaapi={0}{1}", |
| 0 | 879 | | alias, |
| 0 | 880 | | options); |
| | 881 | | } |
| | 882 | |
|
| | 883 | | private string GetDrmDeviceArgs(string renderNodePath, string alias) |
| | 884 | | { |
| 0 | 885 | | alias ??= DrmAlias; |
| 0 | 886 | | renderNodePath = renderNodePath ?? "/dev/dri/renderD128"; |
| | 887 | |
|
| 0 | 888 | | return string.Format( |
| 0 | 889 | | CultureInfo.InvariantCulture, |
| 0 | 890 | | " -init_hw_device drm={0}:{1}", |
| 0 | 891 | | alias, |
| 0 | 892 | | renderNodePath); |
| | 893 | | } |
| | 894 | |
|
| | 895 | | private string GetQsvDeviceArgs(string renderNodePath, string alias) |
| | 896 | | { |
| 0 | 897 | | var arg = " -init_hw_device qsv=" + (alias ?? QsvAlias); |
| 0 | 898 | | if (OperatingSystem.IsLinux()) |
| | 899 | | { |
| | 900 | | // derive qsv from vaapi device |
| 0 | 901 | | return GetVaapiDeviceArgs(renderNodePath, "iHD", "i915", "0x8086", null, VaapiAlias) + arg + "@" + Vaapi |
| | 902 | | } |
| | 903 | |
|
| 0 | 904 | | if (OperatingSystem.IsWindows()) |
| | 905 | | { |
| | 906 | | // on Windows, the deviceIndex is an int |
| 0 | 907 | | if (int.TryParse(renderNodePath, NumberStyles.Integer, CultureInfo.InvariantCulture, out int deviceIndex |
| | 908 | | { |
| 0 | 909 | | return GetD3d11vaDeviceArgs(deviceIndex, string.Empty, D3d11vaAlias) + arg + "@" + D3d11vaAlias; |
| | 910 | | } |
| | 911 | |
|
| | 912 | | // derive qsv from d3d11va device |
| 0 | 913 | | return GetD3d11vaDeviceArgs(0, "0x8086", D3d11vaAlias) + arg + "@" + D3d11vaAlias; |
| | 914 | | } |
| | 915 | |
|
| 0 | 916 | | return null; |
| | 917 | | } |
| | 918 | |
|
| | 919 | | private string GetFilterHwDeviceArgs(string alias) |
| | 920 | | { |
| 0 | 921 | | return string.IsNullOrEmpty(alias) |
| 0 | 922 | | ? string.Empty |
| 0 | 923 | | : " -filter_hw_device " + alias; |
| | 924 | | } |
| | 925 | |
|
| | 926 | | public string GetGraphicalSubCanvasSize(EncodingJobInfo state) |
| | 927 | | { |
| | 928 | | // DVBSUB uses the fixed canvas size 720x576 |
| 0 | 929 | | if (state.SubtitleStream is not null |
| 0 | 930 | | && ShouldEncodeSubtitle(state) |
| 0 | 931 | | && !state.SubtitleStream.IsTextSubtitleStream |
| 0 | 932 | | && !string.Equals(state.SubtitleStream.Codec, "DVBSUB", StringComparison.OrdinalIgnoreCase)) |
| | 933 | | { |
| 0 | 934 | | var subtitleWidth = state.SubtitleStream?.Width; |
| 0 | 935 | | var subtitleHeight = state.SubtitleStream?.Height; |
| | 936 | |
|
| 0 | 937 | | if (subtitleWidth.HasValue |
| 0 | 938 | | && subtitleHeight.HasValue |
| 0 | 939 | | && subtitleWidth.Value > 0 |
| 0 | 940 | | && subtitleHeight.Value > 0) |
| | 941 | | { |
| 0 | 942 | | return string.Format( |
| 0 | 943 | | CultureInfo.InvariantCulture, |
| 0 | 944 | | " -canvas_size {0}x{1}", |
| 0 | 945 | | subtitleWidth.Value, |
| 0 | 946 | | subtitleHeight.Value); |
| | 947 | | } |
| | 948 | | } |
| | 949 | |
|
| 0 | 950 | | return string.Empty; |
| | 951 | | } |
| | 952 | |
|
| | 953 | | /// <summary> |
| | 954 | | /// Gets the input video hwaccel argument. |
| | 955 | | /// </summary> |
| | 956 | | /// <param name="state">Encoding state.</param> |
| | 957 | | /// <param name="options">Encoding options.</param> |
| | 958 | | /// <returns>Input video hwaccel arguments.</returns> |
| | 959 | | public string GetInputVideoHwaccelArgs(EncodingJobInfo state, EncodingOptions options) |
| | 960 | | { |
| 0 | 961 | | if (!state.IsVideoRequest) |
| | 962 | | { |
| 0 | 963 | | return string.Empty; |
| | 964 | | } |
| | 965 | |
|
| 0 | 966 | | var vidEncoder = GetVideoEncoder(state, options) ?? string.Empty; |
| 0 | 967 | | if (IsCopyCodec(vidEncoder)) |
| | 968 | | { |
| 0 | 969 | | return string.Empty; |
| | 970 | | } |
| | 971 | |
|
| 0 | 972 | | var args = new StringBuilder(); |
| 0 | 973 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 974 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 975 | | var isMacOS = OperatingSystem.IsMacOS(); |
| 0 | 976 | | var optHwaccelType = options.HardwareAccelerationType; |
| 0 | 977 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 978 | | var isHwTonemapAvailable = IsHwTonemapAvailable(state, options); |
| | 979 | |
|
| 0 | 980 | | if (optHwaccelType == HardwareAccelerationType.vaapi) |
| | 981 | | { |
| 0 | 982 | | if (!isLinux || !_mediaEncoder.SupportsHwaccel("vaapi")) |
| | 983 | | { |
| 0 | 984 | | return string.Empty; |
| | 985 | | } |
| | 986 | |
|
| 0 | 987 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 988 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 989 | | if (!isVaapiDecoder && !isVaapiEncoder) |
| | 990 | | { |
| 0 | 991 | | return string.Empty; |
| | 992 | | } |
| | 993 | |
|
| 0 | 994 | | if (_mediaEncoder.IsVaapiDeviceInteliHD) |
| | 995 | | { |
| 0 | 996 | | args.Append(GetVaapiDeviceArgs(options.VaapiDevice, "iHD", null, null, null, VaapiAlias)); |
| | 997 | | } |
| 0 | 998 | | else if (_mediaEncoder.IsVaapiDeviceInteli965) |
| | 999 | | { |
| | 1000 | | // Only override i965 since it has lower priority than iHD in libva lookup. |
| 0 | 1001 | | Environment.SetEnvironmentVariable("LIBVA_DRIVER_NAME", "i965"); |
| 0 | 1002 | | Environment.SetEnvironmentVariable("LIBVA_DRIVER_NAME_JELLYFIN", "i965"); |
| 0 | 1003 | | args.Append(GetVaapiDeviceArgs(options.VaapiDevice, "i965", null, null, null, VaapiAlias)); |
| | 1004 | | } |
| | 1005 | |
|
| 0 | 1006 | | var filterDevArgs = string.Empty; |
| 0 | 1007 | | var doOclTonemap = isHwTonemapAvailable && IsOpenclFullSupported(); |
| | 1008 | |
|
| 0 | 1009 | | if (_mediaEncoder.IsVaapiDeviceInteliHD || _mediaEncoder.IsVaapiDeviceInteli965) |
| | 1010 | | { |
| 0 | 1011 | | if (doOclTonemap && !isVaapiDecoder) |
| | 1012 | | { |
| 0 | 1013 | | args.Append(GetOpenclDeviceArgs(0, null, VaapiAlias, OpenclAlias)); |
| 0 | 1014 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1015 | | } |
| | 1016 | | } |
| 0 | 1017 | | else if (_mediaEncoder.IsVaapiDeviceAmd) |
| | 1018 | | { |
| | 1019 | | // Disable AMD EFC feature since it's still unstable in upstream Mesa. |
| 0 | 1020 | | Environment.SetEnvironmentVariable("AMD_DEBUG", "noefc"); |
| | 1021 | |
|
| 0 | 1022 | | if (IsVulkanFullSupported() |
| 0 | 1023 | | && _mediaEncoder.IsVaapiDeviceSupportVulkanDrmInterop |
| 0 | 1024 | | && Environment.OSVersion.Version >= _minKernelVersionAmdVkFmtModifier) |
| | 1025 | | { |
| 0 | 1026 | | args.Append(GetDrmDeviceArgs(options.VaapiDevice, DrmAlias)); |
| 0 | 1027 | | args.Append(GetVaapiDeviceArgs(null, null, null, null, DrmAlias, VaapiAlias)); |
| 0 | 1028 | | args.Append(GetVulkanDeviceArgs(0, null, DrmAlias, VulkanAlias)); |
| | 1029 | |
|
| | 1030 | | // libplacebo wants an explicitly set vulkan filter device. |
| 0 | 1031 | | filterDevArgs = GetFilterHwDeviceArgs(VulkanAlias); |
| | 1032 | | } |
| | 1033 | | else |
| | 1034 | | { |
| 0 | 1035 | | args.Append(GetVaapiDeviceArgs(options.VaapiDevice, null, null, null, null, VaapiAlias)); |
| 0 | 1036 | | filterDevArgs = GetFilterHwDeviceArgs(VaapiAlias); |
| | 1037 | |
|
| 0 | 1038 | | if (doOclTonemap) |
| | 1039 | | { |
| | 1040 | | // ROCm/ROCr OpenCL runtime |
| 0 | 1041 | | args.Append(GetOpenclDeviceArgs(0, "Advanced Micro Devices", null, OpenclAlias)); |
| 0 | 1042 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1043 | | } |
| | 1044 | | } |
| | 1045 | | } |
| 0 | 1046 | | else if (doOclTonemap) |
| | 1047 | | { |
| 0 | 1048 | | args.Append(GetOpenclDeviceArgs(0, null, null, OpenclAlias)); |
| 0 | 1049 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1050 | | } |
| | 1051 | |
|
| 0 | 1052 | | args.Append(filterDevArgs); |
| | 1053 | | } |
| 0 | 1054 | | else if (optHwaccelType == HardwareAccelerationType.qsv) |
| | 1055 | | { |
| 0 | 1056 | | if ((!isLinux && !isWindows) || !_mediaEncoder.SupportsHwaccel("qsv")) |
| | 1057 | | { |
| 0 | 1058 | | return string.Empty; |
| | 1059 | | } |
| | 1060 | |
|
| 0 | 1061 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 1062 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 1063 | | var isQsvDecoder = vidDecoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 1064 | | var isQsvEncoder = vidEncoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 1065 | | var isHwDecoder = isQsvDecoder || isVaapiDecoder || isD3d11vaDecoder; |
| 0 | 1066 | | if (!isHwDecoder && !isQsvEncoder) |
| | 1067 | | { |
| 0 | 1068 | | return string.Empty; |
| | 1069 | | } |
| | 1070 | |
|
| 0 | 1071 | | args.Append(GetQsvDeviceArgs(options.QsvDevice, QsvAlias)); |
| 0 | 1072 | | var filterDevArgs = GetFilterHwDeviceArgs(QsvAlias); |
| | 1073 | | // child device used by qsv. |
| 0 | 1074 | | if (_mediaEncoder.SupportsHwaccel("vaapi") || _mediaEncoder.SupportsHwaccel("d3d11va")) |
| | 1075 | | { |
| 0 | 1076 | | if (isHwTonemapAvailable && IsOpenclFullSupported()) |
| | 1077 | | { |
| 0 | 1078 | | var srcAlias = isLinux ? VaapiAlias : D3d11vaAlias; |
| 0 | 1079 | | args.Append(GetOpenclDeviceArgs(0, null, srcAlias, OpenclAlias)); |
| 0 | 1080 | | if (!isHwDecoder) |
| | 1081 | | { |
| 0 | 1082 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1083 | | } |
| | 1084 | | } |
| | 1085 | | } |
| | 1086 | |
|
| 0 | 1087 | | args.Append(filterDevArgs); |
| | 1088 | | } |
| 0 | 1089 | | else if (optHwaccelType == HardwareAccelerationType.nvenc) |
| | 1090 | | { |
| 0 | 1091 | | if ((!isLinux && !isWindows) || !IsCudaFullSupported()) |
| | 1092 | | { |
| 0 | 1093 | | return string.Empty; |
| | 1094 | | } |
| | 1095 | |
|
| 0 | 1096 | | var isCuvidDecoder = vidDecoder.Contains("cuvid", StringComparison.OrdinalIgnoreCase); |
| 0 | 1097 | | var isNvdecDecoder = vidDecoder.Contains("cuda", StringComparison.OrdinalIgnoreCase); |
| 0 | 1098 | | var isNvencEncoder = vidEncoder.Contains("nvenc", StringComparison.OrdinalIgnoreCase); |
| 0 | 1099 | | var isHwDecoder = isNvdecDecoder || isCuvidDecoder; |
| 0 | 1100 | | if (!isHwDecoder && !isNvencEncoder) |
| | 1101 | | { |
| 0 | 1102 | | return string.Empty; |
| | 1103 | | } |
| | 1104 | |
|
| 0 | 1105 | | args.Append(GetCudaDeviceArgs(0, CudaAlias)) |
| 0 | 1106 | | .Append(GetFilterHwDeviceArgs(CudaAlias)); |
| | 1107 | | } |
| 0 | 1108 | | else if (optHwaccelType == HardwareAccelerationType.amf) |
| | 1109 | | { |
| 0 | 1110 | | if (!isWindows || !_mediaEncoder.SupportsHwaccel("d3d11va")) |
| | 1111 | | { |
| 0 | 1112 | | return string.Empty; |
| | 1113 | | } |
| | 1114 | |
|
| 0 | 1115 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 1116 | | var isAmfEncoder = vidEncoder.Contains("amf", StringComparison.OrdinalIgnoreCase); |
| 0 | 1117 | | if (!isD3d11vaDecoder && !isAmfEncoder) |
| | 1118 | | { |
| 0 | 1119 | | return string.Empty; |
| | 1120 | | } |
| | 1121 | |
|
| | 1122 | | // no dxva video processor hw filter. |
| 0 | 1123 | | args.Append(GetD3d11vaDeviceArgs(0, "0x1002", D3d11vaAlias)); |
| 0 | 1124 | | var filterDevArgs = string.Empty; |
| 0 | 1125 | | if (IsOpenclFullSupported()) |
| | 1126 | | { |
| 0 | 1127 | | args.Append(GetOpenclDeviceArgs(0, null, D3d11vaAlias, OpenclAlias)); |
| 0 | 1128 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1129 | | } |
| | 1130 | |
|
| 0 | 1131 | | args.Append(filterDevArgs); |
| | 1132 | | } |
| 0 | 1133 | | else if (optHwaccelType == HardwareAccelerationType.videotoolbox) |
| | 1134 | | { |
| 0 | 1135 | | if (!isMacOS || !_mediaEncoder.SupportsHwaccel("videotoolbox")) |
| | 1136 | | { |
| 0 | 1137 | | return string.Empty; |
| | 1138 | | } |
| | 1139 | |
|
| 0 | 1140 | | var isVideotoolboxDecoder = vidDecoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 1141 | | var isVideotoolboxEncoder = vidEncoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 1142 | | if (!isVideotoolboxDecoder && !isVideotoolboxEncoder) |
| | 1143 | | { |
| 0 | 1144 | | return string.Empty; |
| | 1145 | | } |
| | 1146 | |
|
| | 1147 | | // videotoolbox hw filter does not require device selection |
| 0 | 1148 | | args.Append(GetVideoToolboxDeviceArgs(VideotoolboxAlias)); |
| | 1149 | | } |
| 0 | 1150 | | else if (optHwaccelType == HardwareAccelerationType.rkmpp) |
| | 1151 | | { |
| 0 | 1152 | | if (!isLinux || !_mediaEncoder.SupportsHwaccel("rkmpp")) |
| | 1153 | | { |
| 0 | 1154 | | return string.Empty; |
| | 1155 | | } |
| | 1156 | |
|
| 0 | 1157 | | var isRkmppDecoder = vidDecoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 1158 | | var isRkmppEncoder = vidEncoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 1159 | | if (!isRkmppDecoder && !isRkmppEncoder) |
| | 1160 | | { |
| 0 | 1161 | | return string.Empty; |
| | 1162 | | } |
| | 1163 | |
|
| 0 | 1164 | | args.Append(GetRkmppDeviceArgs(RkmppAlias)); |
| | 1165 | |
|
| 0 | 1166 | | var filterDevArgs = string.Empty; |
| 0 | 1167 | | var doOclTonemap = isHwTonemapAvailable && IsOpenclFullSupported(); |
| | 1168 | |
|
| 0 | 1169 | | if (doOclTonemap && !isRkmppDecoder) |
| | 1170 | | { |
| 0 | 1171 | | args.Append(GetOpenclDeviceArgs(0, null, RkmppAlias, OpenclAlias)); |
| 0 | 1172 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1173 | | } |
| | 1174 | |
|
| 0 | 1175 | | args.Append(filterDevArgs); |
| | 1176 | | } |
| | 1177 | |
|
| 0 | 1178 | | if (!string.IsNullOrEmpty(vidDecoder)) |
| | 1179 | | { |
| 0 | 1180 | | args.Append(vidDecoder); |
| | 1181 | | } |
| | 1182 | |
|
| 0 | 1183 | | return args.ToString().Trim(); |
| | 1184 | | } |
| | 1185 | |
|
| | 1186 | | /// <summary> |
| | 1187 | | /// Gets the input argument. |
| | 1188 | | /// </summary> |
| | 1189 | | /// <param name="state">Encoding state.</param> |
| | 1190 | | /// <param name="options">Encoding options.</param> |
| | 1191 | | /// <param name="segmentContainer">Segment Container.</param> |
| | 1192 | | /// <returns>Input arguments.</returns> |
| | 1193 | | public string GetInputArgument(EncodingJobInfo state, EncodingOptions options, string segmentContainer) |
| | 1194 | | { |
| 0 | 1195 | | var arg = new StringBuilder(); |
| 0 | 1196 | | var inputVidHwaccelArgs = GetInputVideoHwaccelArgs(state, options); |
| | 1197 | |
|
| 0 | 1198 | | if (!string.IsNullOrEmpty(inputVidHwaccelArgs)) |
| | 1199 | | { |
| 0 | 1200 | | arg.Append(inputVidHwaccelArgs); |
| | 1201 | | } |
| | 1202 | |
|
| 0 | 1203 | | var canvasArgs = GetGraphicalSubCanvasSize(state); |
| 0 | 1204 | | if (!string.IsNullOrEmpty(canvasArgs)) |
| | 1205 | | { |
| 0 | 1206 | | arg.Append(canvasArgs); |
| | 1207 | | } |
| | 1208 | |
|
| 0 | 1209 | | if (state.MediaSource.VideoType == VideoType.Dvd || state.MediaSource.VideoType == VideoType.BluRay) |
| | 1210 | | { |
| 0 | 1211 | | var concatFilePath = Path.Join(_configurationManager.CommonApplicationPaths.CachePath, "concat", state.M |
| 0 | 1212 | | if (!File.Exists(concatFilePath)) |
| | 1213 | | { |
| 0 | 1214 | | _mediaEncoder.GenerateConcatConfig(state.MediaSource, concatFilePath); |
| | 1215 | | } |
| | 1216 | |
|
| 0 | 1217 | | arg.Append(" -f concat -safe 0 -i \"") |
| 0 | 1218 | | .Append(concatFilePath) |
| 0 | 1219 | | .Append("\" "); |
| | 1220 | | } |
| | 1221 | | else |
| | 1222 | | { |
| 0 | 1223 | | arg.Append(" -i ") |
| 0 | 1224 | | .Append(_mediaEncoder.GetInputPathArgument(state)); |
| | 1225 | | } |
| | 1226 | |
|
| | 1227 | | // sub2video for external graphical subtitles |
| 0 | 1228 | | if (state.SubtitleStream is not null |
| 0 | 1229 | | && ShouldEncodeSubtitle(state) |
| 0 | 1230 | | && !state.SubtitleStream.IsTextSubtitleStream |
| 0 | 1231 | | && state.SubtitleStream.IsExternal) |
| | 1232 | | { |
| 0 | 1233 | | var subtitlePath = state.SubtitleStream.Path; |
| 0 | 1234 | | var subtitleExtension = Path.GetExtension(subtitlePath.AsSpan()); |
| | 1235 | |
|
| | 1236 | | // dvdsub/vobsub graphical subtitles use .sub+.idx pairs |
| 0 | 1237 | | if (subtitleExtension.Equals(".sub", StringComparison.OrdinalIgnoreCase)) |
| | 1238 | | { |
| 0 | 1239 | | var idxFile = Path.ChangeExtension(subtitlePath, ".idx"); |
| 0 | 1240 | | if (File.Exists(idxFile)) |
| | 1241 | | { |
| 0 | 1242 | | subtitlePath = idxFile; |
| | 1243 | | } |
| | 1244 | | } |
| | 1245 | |
|
| | 1246 | | // Also seek the external subtitles stream. |
| 0 | 1247 | | var seekSubParam = GetFastSeekCommandLineParameter(state, options, segmentContainer); |
| 0 | 1248 | | if (!string.IsNullOrEmpty(seekSubParam)) |
| | 1249 | | { |
| 0 | 1250 | | arg.Append(' ').Append(seekSubParam); |
| | 1251 | | } |
| | 1252 | |
|
| 0 | 1253 | | if (!string.IsNullOrEmpty(canvasArgs)) |
| | 1254 | | { |
| 0 | 1255 | | arg.Append(canvasArgs); |
| | 1256 | | } |
| | 1257 | |
|
| 0 | 1258 | | arg.Append(" -i file:\"").Append(subtitlePath).Append('\"'); |
| | 1259 | | } |
| | 1260 | |
|
| 0 | 1261 | | if (state.AudioStream is not null && state.AudioStream.IsExternal) |
| | 1262 | | { |
| | 1263 | | // Also seek the external audio stream. |
| 0 | 1264 | | var seekAudioParam = GetFastSeekCommandLineParameter(state, options, segmentContainer); |
| 0 | 1265 | | if (!string.IsNullOrEmpty(seekAudioParam)) |
| | 1266 | | { |
| 0 | 1267 | | arg.Append(' ').Append(seekAudioParam); |
| | 1268 | | } |
| | 1269 | |
|
| 0 | 1270 | | arg.Append(" -i \"").Append(state.AudioStream.Path).Append('"'); |
| | 1271 | | } |
| | 1272 | |
|
| | 1273 | | // Disable auto inserted SW scaler for HW decoders in case of changed resolution. |
| 0 | 1274 | | var isSwDecoder = string.IsNullOrEmpty(GetHardwareVideoDecoder(state, options)); |
| 0 | 1275 | | if (!isSwDecoder) |
| | 1276 | | { |
| 0 | 1277 | | arg.Append(" -noautoscale"); |
| | 1278 | | } |
| | 1279 | |
|
| 0 | 1280 | | return arg.ToString(); |
| | 1281 | | } |
| | 1282 | |
|
| | 1283 | | /// <summary> |
| | 1284 | | /// Determines whether the specified stream is H264. |
| | 1285 | | /// </summary> |
| | 1286 | | /// <param name="stream">The stream.</param> |
| | 1287 | | /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns> |
| | 1288 | | public static bool IsH264(MediaStream stream) |
| | 1289 | | { |
| 0 | 1290 | | var codec = stream.Codec ?? string.Empty; |
| | 1291 | |
|
| 0 | 1292 | | return codec.Contains("264", StringComparison.OrdinalIgnoreCase) |
| 0 | 1293 | | || codec.Contains("avc", StringComparison.OrdinalIgnoreCase); |
| | 1294 | | } |
| | 1295 | |
|
| | 1296 | | public static bool IsH265(MediaStream stream) |
| | 1297 | | { |
| 0 | 1298 | | var codec = stream.Codec ?? string.Empty; |
| | 1299 | |
|
| 0 | 1300 | | return codec.Contains("265", StringComparison.OrdinalIgnoreCase) |
| 0 | 1301 | | || codec.Contains("hevc", StringComparison.OrdinalIgnoreCase); |
| | 1302 | | } |
| | 1303 | |
|
| | 1304 | | public static bool IsAAC(MediaStream stream) |
| | 1305 | | { |
| 0 | 1306 | | var codec = stream.Codec ?? string.Empty; |
| | 1307 | |
|
| 0 | 1308 | | return codec.Contains("aac", StringComparison.OrdinalIgnoreCase); |
| | 1309 | | } |
| | 1310 | |
|
| | 1311 | | public static string GetBitStreamArgs(MediaStream stream) |
| | 1312 | | { |
| | 1313 | | // TODO This is auto inserted into the mpegts mux so it might not be needed. |
| | 1314 | | // https://www.ffmpeg.org/ffmpeg-bitstream-filters.html#h264_005fmp4toannexb |
| 0 | 1315 | | if (IsH264(stream)) |
| | 1316 | | { |
| 0 | 1317 | | return "-bsf:v h264_mp4toannexb"; |
| | 1318 | | } |
| | 1319 | |
|
| 0 | 1320 | | if (IsH265(stream)) |
| | 1321 | | { |
| 0 | 1322 | | return "-bsf:v hevc_mp4toannexb"; |
| | 1323 | | } |
| | 1324 | |
|
| 0 | 1325 | | if (IsAAC(stream)) |
| | 1326 | | { |
| | 1327 | | // Convert adts header(mpegts) to asc header(mp4). |
| 0 | 1328 | | return "-bsf:a aac_adtstoasc"; |
| | 1329 | | } |
| | 1330 | |
|
| 0 | 1331 | | return null; |
| | 1332 | | } |
| | 1333 | |
|
| | 1334 | | public static string GetAudioBitStreamArguments(EncodingJobInfo state, string segmentContainer, string mediaSour |
| | 1335 | | { |
| 0 | 1336 | | var bitStreamArgs = string.Empty; |
| 0 | 1337 | | var segmentFormat = GetSegmentFileExtension(segmentContainer).TrimStart('.'); |
| | 1338 | |
|
| | 1339 | | // Apply aac_adtstoasc bitstream filter when media source is in mpegts. |
| 0 | 1340 | | if (string.Equals(segmentFormat, "mp4", StringComparison.OrdinalIgnoreCase) |
| 0 | 1341 | | && (string.Equals(mediaSourceContainer, "ts", StringComparison.OrdinalIgnoreCase) |
| 0 | 1342 | | || string.Equals(mediaSourceContainer, "aac", StringComparison.OrdinalIgnoreCase) |
| 0 | 1343 | | || string.Equals(mediaSourceContainer, "hls", StringComparison.OrdinalIgnoreCase))) |
| | 1344 | | { |
| 0 | 1345 | | bitStreamArgs = GetBitStreamArgs(state.AudioStream); |
| 0 | 1346 | | bitStreamArgs = string.IsNullOrEmpty(bitStreamArgs) ? string.Empty : " " + bitStreamArgs; |
| | 1347 | | } |
| | 1348 | |
|
| 0 | 1349 | | return bitStreamArgs; |
| | 1350 | | } |
| | 1351 | |
|
| | 1352 | | public static string GetSegmentFileExtension(string segmentContainer) |
| | 1353 | | { |
| 0 | 1354 | | if (!string.IsNullOrWhiteSpace(segmentContainer)) |
| | 1355 | | { |
| 0 | 1356 | | return "." + segmentContainer; |
| | 1357 | | } |
| | 1358 | |
|
| 0 | 1359 | | return ".ts"; |
| | 1360 | | } |
| | 1361 | |
|
| | 1362 | | private string GetVideoBitrateParam(EncodingJobInfo state, string videoCodec) |
| | 1363 | | { |
| 0 | 1364 | | if (state.OutputVideoBitrate is null) |
| | 1365 | | { |
| 0 | 1366 | | return string.Empty; |
| | 1367 | | } |
| | 1368 | |
|
| 0 | 1369 | | int bitrate = state.OutputVideoBitrate.Value; |
| | 1370 | |
|
| | 1371 | | // Bit rate under 1000k is not allowed in h264_qsv |
| 0 | 1372 | | if (string.Equals(videoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase)) |
| | 1373 | | { |
| 0 | 1374 | | bitrate = Math.Max(bitrate, 1000); |
| | 1375 | | } |
| | 1376 | |
|
| | 1377 | | // Currently use the same buffer size for all encoders |
| 0 | 1378 | | int bufsize = bitrate * 2; |
| | 1379 | |
|
| 0 | 1380 | | if (string.Equals(videoCodec, "libsvtav1", StringComparison.OrdinalIgnoreCase)) |
| | 1381 | | { |
| 0 | 1382 | | return FormattableString.Invariant($" -b:v {bitrate} -bufsize {bufsize}"); |
| | 1383 | | } |
| | 1384 | |
|
| 0 | 1385 | | if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 1386 | | || string.Equals(videoCodec, "libx265", StringComparison.OrdinalIgnoreCase)) |
| | 1387 | | { |
| 0 | 1388 | | return FormattableString.Invariant($" -maxrate {bitrate} -bufsize {bufsize}"); |
| | 1389 | | } |
| | 1390 | |
|
| 0 | 1391 | | if (string.Equals(videoCodec, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1392 | | || string.Equals(videoCodec, "hevc_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1393 | | || string.Equals(videoCodec, "av1_amf", StringComparison.OrdinalIgnoreCase)) |
| | 1394 | | { |
| | 1395 | | // Override the too high default qmin 18 in transcoding preset |
| 0 | 1396 | | return FormattableString.Invariant($" -rc cbr -qmin 0 -qmax 32 -b:v {bitrate} -maxrate {bitrate} -bufsiz |
| | 1397 | | } |
| | 1398 | |
|
| 0 | 1399 | | if (string.Equals(videoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1400 | | || string.Equals(videoCodec, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1401 | | || string.Equals(videoCodec, "av1_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1402 | | { |
| | 1403 | | // VBR in i965 driver may result in pixelated output. |
| 0 | 1404 | | if (_mediaEncoder.IsVaapiDeviceInteli965) |
| | 1405 | | { |
| 0 | 1406 | | return FormattableString.Invariant($" -rc_mode CBR -b:v {bitrate} -maxrate {bitrate} -bufsize {bufsi |
| | 1407 | | } |
| | 1408 | |
|
| 0 | 1409 | | return FormattableString.Invariant($" -rc_mode VBR -b:v {bitrate} -maxrate {bitrate} -bufsize {bufsize}" |
| | 1410 | | } |
| | 1411 | |
|
| 0 | 1412 | | if (string.Equals(videoCodec, "h264_videotoolbox", StringComparison.OrdinalIgnoreCase) |
| 0 | 1413 | | || string.Equals(videoCodec, "hevc_videotoolbox", StringComparison.OrdinalIgnoreCase)) |
| | 1414 | | { |
| | 1415 | | // The `maxrate` and `bufsize` options can potentially lead to performance regression |
| | 1416 | | // and even encoder hangs, especially when the value is very high. |
| 0 | 1417 | | return FormattableString.Invariant($" -b:v {bitrate} -qmin -1 -qmax -1"); |
| | 1418 | | } |
| | 1419 | |
|
| 0 | 1420 | | return FormattableString.Invariant($" -b:v {bitrate} -maxrate {bitrate} -bufsize {bufsize}"); |
| | 1421 | | } |
| | 1422 | |
|
| | 1423 | | private string GetEncoderParam(EncoderPreset? preset, EncoderPreset defaultPreset, EncodingOptions encodingOptio |
| | 1424 | | { |
| 0 | 1425 | | var param = string.Empty; |
| 0 | 1426 | | var encoderPreset = preset ?? defaultPreset; |
| 0 | 1427 | | if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase) || isLibX265) |
| | 1428 | | { |
| 0 | 1429 | | var presetString = encoderPreset switch |
| 0 | 1430 | | { |
| 0 | 1431 | | EncoderPreset.auto => EncoderPreset.veryfast.ToString().ToLowerInvariant(), |
| 0 | 1432 | | _ => encoderPreset.ToString().ToLowerInvariant() |
| 0 | 1433 | | }; |
| | 1434 | |
|
| 0 | 1435 | | param += " -preset " + presetString; |
| | 1436 | |
|
| 0 | 1437 | | int encodeCrf = encodingOptions.H264Crf; |
| 0 | 1438 | | if (isLibX265) |
| | 1439 | | { |
| 0 | 1440 | | encodeCrf = encodingOptions.H265Crf; |
| | 1441 | | } |
| | 1442 | |
|
| 0 | 1443 | | if (encodeCrf >= 0 && encodeCrf <= 51) |
| | 1444 | | { |
| 0 | 1445 | | param += " -crf " + encodeCrf.ToString(CultureInfo.InvariantCulture); |
| | 1446 | | } |
| | 1447 | | else |
| | 1448 | | { |
| 0 | 1449 | | string defaultCrf = "23"; |
| 0 | 1450 | | if (isLibX265) |
| | 1451 | | { |
| 0 | 1452 | | defaultCrf = "28"; |
| | 1453 | | } |
| | 1454 | |
|
| 0 | 1455 | | param += " -crf " + defaultCrf; |
| | 1456 | | } |
| | 1457 | | } |
| 0 | 1458 | | else if (string.Equals(videoEncoder, "libsvtav1", StringComparison.OrdinalIgnoreCase)) |
| | 1459 | | { |
| | 1460 | | // Default to use the recommended preset 10. |
| | 1461 | | // Omit presets < 5, which are too slow for on the fly encoding. |
| | 1462 | | // https://gitlab.com/AOMediaCodec/SVT-AV1/-/blob/master/Docs/Ffmpeg.md |
| 0 | 1463 | | param += encoderPreset switch |
| 0 | 1464 | | { |
| 0 | 1465 | | EncoderPreset.veryslow => " -preset 5", |
| 0 | 1466 | | EncoderPreset.slower => " -preset 6", |
| 0 | 1467 | | EncoderPreset.slow => " -preset 7", |
| 0 | 1468 | | EncoderPreset.medium => " -preset 8", |
| 0 | 1469 | | EncoderPreset.fast => " -preset 9", |
| 0 | 1470 | | EncoderPreset.faster => " -preset 10", |
| 0 | 1471 | | EncoderPreset.veryfast => " -preset 11", |
| 0 | 1472 | | EncoderPreset.superfast => " -preset 12", |
| 0 | 1473 | | EncoderPreset.ultrafast => " -preset 13", |
| 0 | 1474 | | _ => " -preset 10" |
| 0 | 1475 | | }; |
| | 1476 | | } |
| 0 | 1477 | | else if (string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1478 | | || string.Equals(videoEncoder, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1479 | | || string.Equals(videoEncoder, "av1_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1480 | | { |
| | 1481 | | // -compression_level is not reliable on AMD. |
| 0 | 1482 | | if (_mediaEncoder.IsVaapiDeviceInteliHD) |
| | 1483 | | { |
| 0 | 1484 | | param += encoderPreset switch |
| 0 | 1485 | | { |
| 0 | 1486 | | EncoderPreset.veryslow => " -compression_level 1", |
| 0 | 1487 | | EncoderPreset.slower => " -compression_level 2", |
| 0 | 1488 | | EncoderPreset.slow => " -compression_level 3", |
| 0 | 1489 | | EncoderPreset.medium => " -compression_level 4", |
| 0 | 1490 | | EncoderPreset.fast => " -compression_level 5", |
| 0 | 1491 | | EncoderPreset.faster => " -compression_level 6", |
| 0 | 1492 | | EncoderPreset.veryfast => " -compression_level 7", |
| 0 | 1493 | | EncoderPreset.superfast => " -compression_level 7", |
| 0 | 1494 | | EncoderPreset.ultrafast => " -compression_level 7", |
| 0 | 1495 | | _ => string.Empty |
| 0 | 1496 | | }; |
| | 1497 | | } |
| | 1498 | | } |
| 0 | 1499 | | else if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) // h264 (h264_qsv) |
| 0 | 1500 | | || string.Equals(videoEncoder, "hevc_qsv", StringComparison.OrdinalIgnoreCase) // hevc (hevc_qsv) |
| 0 | 1501 | | || string.Equals(videoEncoder, "av1_qsv", StringComparison.OrdinalIgnoreCase)) // av1 (av1_qsv) |
| | 1502 | | { |
| 0 | 1503 | | EncoderPreset[] valid_presets = [EncoderPreset.veryslow, EncoderPreset.slower, EncoderPreset.slow, Encod |
| | 1504 | |
|
| 0 | 1505 | | param += " -preset " + (valid_presets.Contains(encoderPreset) ? encoderPreset : EncoderPreset.veryfast). |
| | 1506 | | } |
| 0 | 1507 | | else if (string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) // h264 (h264_nvenc) |
| 0 | 1508 | | || string.Equals(videoEncoder, "hevc_nvenc", StringComparison.OrdinalIgnoreCase) // hevc (hevc_n |
| 0 | 1509 | | || string.Equals(videoEncoder, "av1_nvenc", StringComparison.OrdinalIgnoreCase) // av1 (av1_nven |
| 0 | 1510 | | ) |
| | 1511 | | { |
| 0 | 1512 | | param += encoderPreset switch |
| 0 | 1513 | | { |
| 0 | 1514 | | EncoderPreset.veryslow => " -preset p7", |
| 0 | 1515 | | EncoderPreset.slower => " -preset p6", |
| 0 | 1516 | | EncoderPreset.slow => " -preset p5", |
| 0 | 1517 | | EncoderPreset.medium => " -preset p4", |
| 0 | 1518 | | EncoderPreset.fast => " -preset p3", |
| 0 | 1519 | | EncoderPreset.faster => " -preset p2", |
| 0 | 1520 | | _ => " -preset p1" |
| 0 | 1521 | | }; |
| | 1522 | | } |
| 0 | 1523 | | else if (string.Equals(videoEncoder, "h264_amf", StringComparison.OrdinalIgnoreCase) // h264 (h264_amf) |
| 0 | 1524 | | || string.Equals(videoEncoder, "hevc_amf", StringComparison.OrdinalIgnoreCase) // hevc (hevc_amf |
| 0 | 1525 | | || string.Equals(videoEncoder, "av1_amf", StringComparison.OrdinalIgnoreCase) // av1 (av1_amf) |
| 0 | 1526 | | ) |
| | 1527 | | { |
| 0 | 1528 | | param += encoderPreset switch |
| 0 | 1529 | | { |
| 0 | 1530 | | EncoderPreset.veryslow => " -quality quality", |
| 0 | 1531 | | EncoderPreset.slower => " -quality quality", |
| 0 | 1532 | | EncoderPreset.slow => " -quality quality", |
| 0 | 1533 | | EncoderPreset.medium => " -quality balanced", |
| 0 | 1534 | | _ => " -quality speed" |
| 0 | 1535 | | }; |
| | 1536 | |
|
| 0 | 1537 | | if (string.Equals(videoEncoder, "hevc_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1538 | | || string.Equals(videoEncoder, "av1_amf", StringComparison.OrdinalIgnoreCase)) |
| | 1539 | | { |
| 0 | 1540 | | param += " -header_insertion_mode gop"; |
| | 1541 | | } |
| | 1542 | |
|
| 0 | 1543 | | if (string.Equals(videoEncoder, "hevc_amf", StringComparison.OrdinalIgnoreCase)) |
| | 1544 | | { |
| 0 | 1545 | | param += " -gops_per_idr 1"; |
| | 1546 | | } |
| | 1547 | | } |
| 0 | 1548 | | else if (string.Equals(videoEncoder, "h264_videotoolbox", StringComparison.OrdinalIgnoreCase) // h264 (h264_ |
| 0 | 1549 | | || string.Equals(videoEncoder, "hevc_videotoolbox", StringComparison.OrdinalIgnoreCase) // hevc |
| 0 | 1550 | | ) |
| | 1551 | | { |
| 0 | 1552 | | param += encoderPreset switch |
| 0 | 1553 | | { |
| 0 | 1554 | | EncoderPreset.veryslow => " -prio_speed 0", |
| 0 | 1555 | | EncoderPreset.slower => " -prio_speed 0", |
| 0 | 1556 | | EncoderPreset.slow => " -prio_speed 0", |
| 0 | 1557 | | EncoderPreset.medium => " -prio_speed 0", |
| 0 | 1558 | | _ => " -prio_speed 1" |
| 0 | 1559 | | }; |
| | 1560 | | } |
| | 1561 | |
|
| 0 | 1562 | | return param; |
| | 1563 | | } |
| | 1564 | |
|
| | 1565 | | public static string NormalizeTranscodingLevel(EncodingJobInfo state, string level) |
| | 1566 | | { |
| 0 | 1567 | | if (double.TryParse(level, CultureInfo.InvariantCulture, out double requestLevel)) |
| | 1568 | | { |
| 0 | 1569 | | if (string.Equals(state.ActualOutputVideoCodec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 1570 | | { |
| | 1571 | | // Transcode to level 5.3 (15) and lower for maximum compatibility. |
| | 1572 | | // https://en.wikipedia.org/wiki/AV1#Levels |
| 0 | 1573 | | if (requestLevel < 0 || requestLevel >= 15) |
| | 1574 | | { |
| 0 | 1575 | | return "15"; |
| | 1576 | | } |
| | 1577 | | } |
| 0 | 1578 | | else if (string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1579 | | || string.Equals(state.ActualOutputVideoCodec, "h265", StringComparison.OrdinalIgnoreCase)) |
| | 1580 | | { |
| | 1581 | | // Transcode to level 5.0 and lower for maximum compatibility. |
| | 1582 | | // Level 5.0 is suitable for up to 4k 30fps hevc encoding, otherwise let the encoder to handle it. |
| | 1583 | | // https://en.wikipedia.org/wiki/High_Efficiency_Video_Coding_tiers_and_levels |
| | 1584 | | // MaxLumaSampleRate = 3840*2160*30 = 248832000 < 267386880. |
| 0 | 1585 | | if (requestLevel < 0 || requestLevel >= 150) |
| | 1586 | | { |
| 0 | 1587 | | return "150"; |
| | 1588 | | } |
| | 1589 | | } |
| 0 | 1590 | | else if (string.Equals(state.ActualOutputVideoCodec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 1591 | | { |
| | 1592 | | // Transcode to level 5.1 and lower for maximum compatibility. |
| | 1593 | | // h264 4k 30fps requires at least level 5.1 otherwise it will break on safari fmp4. |
| | 1594 | | // https://en.wikipedia.org/wiki/Advanced_Video_Coding#Levels |
| 0 | 1595 | | if (requestLevel < 0 || requestLevel >= 51) |
| | 1596 | | { |
| 0 | 1597 | | return "51"; |
| | 1598 | | } |
| | 1599 | | } |
| | 1600 | | } |
| | 1601 | |
|
| 0 | 1602 | | return level; |
| | 1603 | | } |
| | 1604 | |
|
| | 1605 | | /// <summary> |
| | 1606 | | /// Gets the text subtitle param. |
| | 1607 | | /// </summary> |
| | 1608 | | /// <param name="state">The state.</param> |
| | 1609 | | /// <param name="enableAlpha">Enable alpha processing.</param> |
| | 1610 | | /// <param name="enableSub2video">Enable sub2video mode.</param> |
| | 1611 | | /// <returns>System.String.</returns> |
| | 1612 | | public string GetTextSubtitlesFilter(EncodingJobInfo state, bool enableAlpha, bool enableSub2video) |
| | 1613 | | { |
| 0 | 1614 | | var seconds = Math.Round(TimeSpan.FromTicks(state.StartTimeTicks ?? 0).TotalSeconds); |
| | 1615 | |
|
| | 1616 | | // hls always copies timestamps |
| 0 | 1617 | | var setPtsParam = state.CopyTimestamps || state.TranscodingType != TranscodingJobType.Progressive |
| 0 | 1618 | | ? string.Empty |
| 0 | 1619 | | : string.Format(CultureInfo.InvariantCulture, ",setpts=PTS -{0}/TB", seconds); |
| | 1620 | |
|
| 0 | 1621 | | var alphaParam = enableAlpha ? ":alpha=1" : string.Empty; |
| 0 | 1622 | | var sub2videoParam = enableSub2video ? ":sub2video=1" : string.Empty; |
| | 1623 | |
|
| 0 | 1624 | | var fontPath = Path.Combine(_appPaths.CachePath, "attachments", state.MediaSource.Id); |
| 0 | 1625 | | var fontParam = string.Format( |
| 0 | 1626 | | CultureInfo.InvariantCulture, |
| 0 | 1627 | | ":fontsdir='{0}'", |
| 0 | 1628 | | _mediaEncoder.EscapeSubtitleFilterPath(fontPath)); |
| | 1629 | |
|
| 0 | 1630 | | if (state.SubtitleStream.IsExternal) |
| | 1631 | | { |
| 0 | 1632 | | var charsetParam = string.Empty; |
| | 1633 | |
|
| 0 | 1634 | | if (!string.IsNullOrEmpty(state.SubtitleStream.Language)) |
| | 1635 | | { |
| 0 | 1636 | | var charenc = _subtitleEncoder.GetSubtitleFileCharacterSet( |
| 0 | 1637 | | state.SubtitleStream, |
| 0 | 1638 | | state.SubtitleStream.Language, |
| 0 | 1639 | | state.MediaSource, |
| 0 | 1640 | | CancellationToken.None).GetAwaiter().GetResult(); |
| | 1641 | |
|
| 0 | 1642 | | if (!string.IsNullOrEmpty(charenc)) |
| | 1643 | | { |
| 0 | 1644 | | charsetParam = ":charenc=" + charenc; |
| | 1645 | | } |
| | 1646 | | } |
| | 1647 | |
|
| 0 | 1648 | | return string.Format( |
| 0 | 1649 | | CultureInfo.InvariantCulture, |
| 0 | 1650 | | "subtitles=f='{0}'{1}{2}{3}{4}{5}", |
| 0 | 1651 | | _mediaEncoder.EscapeSubtitleFilterPath(state.SubtitleStream.Path), |
| 0 | 1652 | | charsetParam, |
| 0 | 1653 | | alphaParam, |
| 0 | 1654 | | sub2videoParam, |
| 0 | 1655 | | fontParam, |
| 0 | 1656 | | setPtsParam); |
| | 1657 | | } |
| | 1658 | |
|
| 0 | 1659 | | var subtitlePath = _subtitleEncoder.GetSubtitleFilePath( |
| 0 | 1660 | | state.SubtitleStream, |
| 0 | 1661 | | state.MediaSource, |
| 0 | 1662 | | CancellationToken.None).GetAwaiter().GetResult(); |
| | 1663 | |
|
| 0 | 1664 | | return string.Format( |
| 0 | 1665 | | CultureInfo.InvariantCulture, |
| 0 | 1666 | | "subtitles=f='{0}'{1}{2}{3}{4}", |
| 0 | 1667 | | _mediaEncoder.EscapeSubtitleFilterPath(subtitlePath), |
| 0 | 1668 | | alphaParam, |
| 0 | 1669 | | sub2videoParam, |
| 0 | 1670 | | fontParam, |
| 0 | 1671 | | setPtsParam); |
| | 1672 | | } |
| | 1673 | |
|
| | 1674 | | public double? GetFramerateParam(EncodingJobInfo state) |
| | 1675 | | { |
| 0 | 1676 | | var request = state.BaseRequest; |
| | 1677 | |
|
| 0 | 1678 | | if (request.Framerate.HasValue) |
| | 1679 | | { |
| 0 | 1680 | | return request.Framerate.Value; |
| | 1681 | | } |
| | 1682 | |
|
| 0 | 1683 | | var maxrate = request.MaxFramerate; |
| | 1684 | |
|
| 0 | 1685 | | if (maxrate.HasValue && state.VideoStream is not null) |
| | 1686 | | { |
| 0 | 1687 | | var contentRate = state.VideoStream.ReferenceFrameRate; |
| | 1688 | |
|
| 0 | 1689 | | if (contentRate.HasValue && contentRate.Value > maxrate.Value) |
| | 1690 | | { |
| 0 | 1691 | | return maxrate; |
| | 1692 | | } |
| | 1693 | | } |
| | 1694 | |
|
| 0 | 1695 | | return null; |
| | 1696 | | } |
| | 1697 | |
|
| | 1698 | | public string GetHlsVideoKeyFrameArguments( |
| | 1699 | | EncodingJobInfo state, |
| | 1700 | | string codec, |
| | 1701 | | int segmentLength, |
| | 1702 | | bool isEventPlaylist, |
| | 1703 | | int? startNumber) |
| | 1704 | | { |
| 0 | 1705 | | var args = string.Empty; |
| 0 | 1706 | | var gopArg = string.Empty; |
| | 1707 | |
|
| 0 | 1708 | | var keyFrameArg = string.Format( |
| 0 | 1709 | | CultureInfo.InvariantCulture, |
| 0 | 1710 | | " -force_key_frames:0 \"expr:gte(t,n_forced*{0})\"", |
| 0 | 1711 | | segmentLength); |
| | 1712 | |
|
| 0 | 1713 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 1714 | | if (framerate.HasValue) |
| | 1715 | | { |
| | 1716 | | // This is to make sure keyframe interval is limited to our segment, |
| | 1717 | | // as forcing keyframes is not enough. |
| | 1718 | | // Example: we encoded half of desired length, then codec detected |
| | 1719 | | // scene cut and inserted a keyframe; next forced keyframe would |
| | 1720 | | // be created outside of segment, which breaks seeking. |
| 0 | 1721 | | gopArg = string.Format( |
| 0 | 1722 | | CultureInfo.InvariantCulture, |
| 0 | 1723 | | " -g:v:0 {0} -keyint_min:v:0 {0}", |
| 0 | 1724 | | Math.Ceiling(segmentLength * framerate.Value)); |
| | 1725 | | } |
| | 1726 | |
|
| | 1727 | | // Unable to force key frames using these encoders, set key frames by GOP. |
| 0 | 1728 | | if (string.Equals(codec, "h264_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1729 | | || string.Equals(codec, "h264_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1730 | | || string.Equals(codec, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1731 | | || string.Equals(codec, "h264_rkmpp", StringComparison.OrdinalIgnoreCase) |
| 0 | 1732 | | || string.Equals(codec, "hevc_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1733 | | || string.Equals(codec, "hevc_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1734 | | || string.Equals(codec, "hevc_rkmpp", StringComparison.OrdinalIgnoreCase) |
| 0 | 1735 | | || string.Equals(codec, "av1_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1736 | | || string.Equals(codec, "av1_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1737 | | || string.Equals(codec, "av1_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1738 | | || string.Equals(codec, "libsvtav1", StringComparison.OrdinalIgnoreCase)) |
| | 1739 | | { |
| 0 | 1740 | | args += gopArg; |
| | 1741 | | } |
| 0 | 1742 | | else if (string.Equals(codec, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 1743 | | || string.Equals(codec, "libx265", StringComparison.OrdinalIgnoreCase) |
| 0 | 1744 | | || string.Equals(codec, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1745 | | || string.Equals(codec, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1746 | | || string.Equals(codec, "av1_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1747 | | { |
| 0 | 1748 | | args += keyFrameArg; |
| | 1749 | |
|
| | 1750 | | // prevent the libx264 from post processing to break the set keyframe. |
| 0 | 1751 | | if (string.Equals(codec, "libx264", StringComparison.OrdinalIgnoreCase)) |
| | 1752 | | { |
| 0 | 1753 | | args += " -sc_threshold:v:0 0"; |
| | 1754 | | } |
| | 1755 | | } |
| | 1756 | | else |
| | 1757 | | { |
| 0 | 1758 | | args += keyFrameArg + gopArg; |
| | 1759 | | } |
| | 1760 | |
|
| | 1761 | | // global_header produced by AMD HEVC VA-API encoder causes non-playable fMP4 on iOS |
| 0 | 1762 | | if (string.Equals(codec, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1763 | | && _mediaEncoder.IsVaapiDeviceAmd) |
| | 1764 | | { |
| 0 | 1765 | | args += " -flags:v -global_header"; |
| | 1766 | | } |
| | 1767 | |
|
| 0 | 1768 | | return args; |
| | 1769 | | } |
| | 1770 | |
|
| | 1771 | | /// <summary> |
| | 1772 | | /// Gets the video bitrate to specify on the command line. |
| | 1773 | | /// </summary> |
| | 1774 | | /// <param name="state">Encoding state.</param> |
| | 1775 | | /// <param name="videoEncoder">Video encoder to use.</param> |
| | 1776 | | /// <param name="encodingOptions">Encoding options.</param> |
| | 1777 | | /// <param name="defaultPreset">Default present to use for encoding.</param> |
| | 1778 | | /// <returns>Video bitrate.</returns> |
| | 1779 | | public string GetVideoQualityParam(EncodingJobInfo state, string videoEncoder, EncodingOptions encodingOptions, |
| | 1780 | | { |
| 0 | 1781 | | var param = string.Empty; |
| | 1782 | |
|
| | 1783 | | // Tutorials: Enable Intel GuC / HuC firmware loading for Low Power Encoding. |
| | 1784 | | // https://01.org/group/43/downloads/firmware |
| | 1785 | | // https://wiki.archlinux.org/title/intel_graphics#Enable_GuC_/_HuC_firmware_loading |
| | 1786 | | // Intel Low Power Encoding can save unnecessary CPU-GPU synchronization, |
| | 1787 | | // which will reduce overhead in performance intensive tasks such as 4k transcoding and tonemapping. |
| 0 | 1788 | | var intelLowPowerHwEncoding = false; |
| | 1789 | |
|
| | 1790 | | // Workaround for linux 5.18 to 6.1.3 i915 hang at cost of performance. |
| | 1791 | | // https://github.com/intel/media-driver/issues/1456 |
| 0 | 1792 | | var enableWaFori915Hang = false; |
| | 1793 | |
|
| 0 | 1794 | | var hardwareAccelerationType = encodingOptions.HardwareAccelerationType; |
| | 1795 | |
|
| 0 | 1796 | | if (hardwareAccelerationType == HardwareAccelerationType.vaapi) |
| | 1797 | | { |
| 0 | 1798 | | var isIntelVaapiDriver = _mediaEncoder.IsVaapiDeviceInteliHD || _mediaEncoder.IsVaapiDeviceInteli965; |
| | 1799 | |
|
| 0 | 1800 | | if (string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1801 | | { |
| 0 | 1802 | | intelLowPowerHwEncoding = encodingOptions.EnableIntelLowPowerH264HwEncoder && isIntelVaapiDriver; |
| | 1803 | | } |
| 0 | 1804 | | else if (string.Equals(videoEncoder, "hevc_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1805 | | { |
| 0 | 1806 | | intelLowPowerHwEncoding = encodingOptions.EnableIntelLowPowerHevcHwEncoder && isIntelVaapiDriver; |
| | 1807 | | } |
| | 1808 | | } |
| 0 | 1809 | | else if (hardwareAccelerationType == HardwareAccelerationType.qsv) |
| | 1810 | | { |
| 0 | 1811 | | if (OperatingSystem.IsLinux()) |
| | 1812 | | { |
| 0 | 1813 | | var ver = Environment.OSVersion.Version; |
| 0 | 1814 | | var isFixedKernel60 = ver.Major == 6 && ver.Minor == 0 && ver >= _minFixedKernel60i915Hang; |
| 0 | 1815 | | var isUnaffectedKernel = ver < _minKerneli915Hang || ver > _maxKerneli915Hang; |
| | 1816 | |
|
| 0 | 1817 | | if (!(isUnaffectedKernel || isFixedKernel60)) |
| | 1818 | | { |
| 0 | 1819 | | var vidDecoder = GetHardwareVideoDecoder(state, encodingOptions) ?? string.Empty; |
| 0 | 1820 | | var isIntelDecoder = vidDecoder.Contains("qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1821 | | || vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 1822 | | var doOclTonemap = _mediaEncoder.SupportsHwaccel("qsv") |
| 0 | 1823 | | && IsVaapiSupported(state) |
| 0 | 1824 | | && IsOpenclFullSupported() |
| 0 | 1825 | | && !IsIntelVppTonemapAvailable(state, encodingOptions) |
| 0 | 1826 | | && IsHwTonemapAvailable(state, encodingOptions); |
| | 1827 | |
|
| 0 | 1828 | | enableWaFori915Hang = isIntelDecoder && doOclTonemap; |
| | 1829 | | } |
| | 1830 | | } |
| | 1831 | |
|
| 0 | 1832 | | if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase)) |
| | 1833 | | { |
| 0 | 1834 | | intelLowPowerHwEncoding = encodingOptions.EnableIntelLowPowerH264HwEncoder; |
| | 1835 | | } |
| 0 | 1836 | | else if (string.Equals(videoEncoder, "hevc_qsv", StringComparison.OrdinalIgnoreCase)) |
| | 1837 | | { |
| 0 | 1838 | | intelLowPowerHwEncoding = encodingOptions.EnableIntelLowPowerHevcHwEncoder; |
| | 1839 | | } |
| | 1840 | | else |
| | 1841 | | { |
| 0 | 1842 | | enableWaFori915Hang = false; |
| | 1843 | | } |
| | 1844 | | } |
| | 1845 | |
|
| 0 | 1846 | | if (intelLowPowerHwEncoding) |
| | 1847 | | { |
| 0 | 1848 | | param += " -low_power 1"; |
| | 1849 | | } |
| | 1850 | |
|
| 0 | 1851 | | if (enableWaFori915Hang) |
| | 1852 | | { |
| 0 | 1853 | | param += " -async_depth 1"; |
| | 1854 | | } |
| | 1855 | |
|
| 0 | 1856 | | var isLibX265 = string.Equals(videoEncoder, "libx265", StringComparison.OrdinalIgnoreCase); |
| 0 | 1857 | | var encodingPreset = encodingOptions.EncoderPreset; |
| | 1858 | |
|
| 0 | 1859 | | param += GetEncoderParam(encodingPreset, defaultPreset, encodingOptions, videoEncoder, isLibX265); |
| 0 | 1860 | | param += GetVideoBitrateParam(state, videoEncoder); |
| | 1861 | |
|
| 0 | 1862 | | var framerate = GetFramerateParam(state); |
| 0 | 1863 | | if (framerate.HasValue) |
| | 1864 | | { |
| 0 | 1865 | | param += string.Format(CultureInfo.InvariantCulture, " -r {0}", framerate.Value.ToString(CultureInfo.Inv |
| | 1866 | | } |
| | 1867 | |
|
| 0 | 1868 | | var targetVideoCodec = state.ActualOutputVideoCodec; |
| 0 | 1869 | | if (string.Equals(targetVideoCodec, "h265", StringComparison.OrdinalIgnoreCase) |
| 0 | 1870 | | || string.Equals(targetVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | 1871 | | { |
| 0 | 1872 | | targetVideoCodec = "hevc"; |
| | 1873 | | } |
| | 1874 | |
|
| 0 | 1875 | | var profile = state.GetRequestedProfiles(targetVideoCodec).FirstOrDefault() ?? string.Empty; |
| 0 | 1876 | | profile = WhiteSpaceRegex().Replace(profile, string.Empty).ToLowerInvariant(); |
| | 1877 | |
|
| 0 | 1878 | | var videoProfiles = Array.Empty<string>(); |
| 0 | 1879 | | if (string.Equals("h264", targetVideoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 1880 | | { |
| 0 | 1881 | | videoProfiles = _videoProfilesH264; |
| | 1882 | | } |
| 0 | 1883 | | else if (string.Equals("hevc", targetVideoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 1884 | | { |
| 0 | 1885 | | videoProfiles = _videoProfilesH265; |
| | 1886 | | } |
| 0 | 1887 | | else if (string.Equals("av1", targetVideoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 1888 | | { |
| 0 | 1889 | | videoProfiles = _videoProfilesAv1; |
| | 1890 | | } |
| | 1891 | |
|
| 0 | 1892 | | if (!videoProfiles.Contains(profile, StringComparison.OrdinalIgnoreCase)) |
| | 1893 | | { |
| 0 | 1894 | | profile = string.Empty; |
| | 1895 | | } |
| | 1896 | |
|
| | 1897 | | // We only transcode to HEVC 8-bit for now, force Main Profile. |
| 0 | 1898 | | if (profile.Contains("main10", StringComparison.OrdinalIgnoreCase) |
| 0 | 1899 | | || profile.Contains("mainstill", StringComparison.OrdinalIgnoreCase)) |
| | 1900 | | { |
| 0 | 1901 | | profile = "main"; |
| | 1902 | | } |
| | 1903 | |
|
| | 1904 | | // Extended Profile is not supported by any known h264 encoders, force Main Profile. |
| 0 | 1905 | | if (profile.Contains("extended", StringComparison.OrdinalIgnoreCase)) |
| | 1906 | | { |
| 0 | 1907 | | profile = "main"; |
| | 1908 | | } |
| | 1909 | |
|
| | 1910 | | // Only libx264 support encoding H264 High 10 Profile, otherwise force High Profile. |
| 0 | 1911 | | if (!string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 1912 | | && profile.Contains("high10", StringComparison.OrdinalIgnoreCase)) |
| | 1913 | | { |
| 0 | 1914 | | profile = "high"; |
| | 1915 | | } |
| | 1916 | |
|
| | 1917 | | // We only need Main profile of AV1 encoders. |
| 0 | 1918 | | if (videoEncoder.Contains("av1", StringComparison.OrdinalIgnoreCase) |
| 0 | 1919 | | && (profile.Contains("high", StringComparison.OrdinalIgnoreCase) |
| 0 | 1920 | | || profile.Contains("professional", StringComparison.OrdinalIgnoreCase))) |
| | 1921 | | { |
| 0 | 1922 | | profile = "main"; |
| | 1923 | | } |
| | 1924 | |
|
| | 1925 | | // h264_vaapi does not support Baseline profile, force Constrained Baseline in this case, |
| | 1926 | | // which is compatible (and ugly). |
| 0 | 1927 | | if (string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1928 | | && profile.Contains("baseline", StringComparison.OrdinalIgnoreCase)) |
| | 1929 | | { |
| 0 | 1930 | | profile = "constrained_baseline"; |
| | 1931 | | } |
| | 1932 | |
|
| | 1933 | | // libx264, h264_{qsv,nvenc,rkmpp} does not support Constrained Baseline profile, force Baseline in this cas |
| 0 | 1934 | | if ((string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 1935 | | || string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1936 | | || string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1937 | | || string.Equals(videoEncoder, "h264_rkmpp", StringComparison.OrdinalIgnoreCase)) |
| 0 | 1938 | | && profile.Contains("baseline", StringComparison.OrdinalIgnoreCase)) |
| | 1939 | | { |
| 0 | 1940 | | profile = "baseline"; |
| | 1941 | | } |
| | 1942 | |
|
| | 1943 | | // libx264, h264_{qsv,nvenc,vaapi,rkmpp} does not support Constrained High profile, force High in this case. |
| 0 | 1944 | | if ((string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 1945 | | || string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1946 | | || string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1947 | | || string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1948 | | || string.Equals(videoEncoder, "h264_rkmpp", StringComparison.OrdinalIgnoreCase)) |
| 0 | 1949 | | && profile.Contains("high", StringComparison.OrdinalIgnoreCase)) |
| | 1950 | | { |
| 0 | 1951 | | profile = "high"; |
| | 1952 | | } |
| | 1953 | |
|
| 0 | 1954 | | if (string.Equals(videoEncoder, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1955 | | && profile.Contains("baseline", StringComparison.OrdinalIgnoreCase)) |
| | 1956 | | { |
| 0 | 1957 | | profile = "constrained_baseline"; |
| | 1958 | | } |
| | 1959 | |
|
| 0 | 1960 | | if (string.Equals(videoEncoder, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1961 | | && profile.Contains("constrainedhigh", StringComparison.OrdinalIgnoreCase)) |
| | 1962 | | { |
| 0 | 1963 | | profile = "constrained_high"; |
| | 1964 | | } |
| | 1965 | |
|
| 0 | 1966 | | if (string.Equals(videoEncoder, "h264_videotoolbox", StringComparison.OrdinalIgnoreCase) |
| 0 | 1967 | | && profile.Contains("constrainedbaseline", StringComparison.OrdinalIgnoreCase)) |
| | 1968 | | { |
| 0 | 1969 | | profile = "constrained_baseline"; |
| | 1970 | | } |
| | 1971 | |
|
| 0 | 1972 | | if (string.Equals(videoEncoder, "h264_videotoolbox", StringComparison.OrdinalIgnoreCase) |
| 0 | 1973 | | && profile.Contains("constrainedhigh", StringComparison.OrdinalIgnoreCase)) |
| | 1974 | | { |
| 0 | 1975 | | profile = "constrained_high"; |
| | 1976 | | } |
| | 1977 | |
|
| 0 | 1978 | | if (!string.IsNullOrEmpty(profile)) |
| | 1979 | | { |
| | 1980 | | // Currently there's no profile option in av1_nvenc encoder |
| 0 | 1981 | | if (!(string.Equals(videoEncoder, "av1_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1982 | | || string.Equals(videoEncoder, "h264_v4l2m2m", StringComparison.OrdinalIgnoreCase))) |
| | 1983 | | { |
| 0 | 1984 | | param += " -profile:v:0 " + profile; |
| | 1985 | | } |
| | 1986 | | } |
| | 1987 | |
|
| 0 | 1988 | | var level = state.GetRequestedLevel(targetVideoCodec); |
| | 1989 | |
|
| 0 | 1990 | | if (!string.IsNullOrEmpty(level)) |
| | 1991 | | { |
| 0 | 1992 | | level = NormalizeTranscodingLevel(state, level); |
| | 1993 | |
|
| | 1994 | | // libx264, QSV, AMF can adjust the given level to match the output. |
| 0 | 1995 | | if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1996 | | || string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase)) |
| | 1997 | | { |
| 0 | 1998 | | param += " -level " + level; |
| | 1999 | | } |
| 0 | 2000 | | else if (string.Equals(videoEncoder, "hevc_qsv", StringComparison.OrdinalIgnoreCase)) |
| | 2001 | | { |
| | 2002 | | // hevc_qsv use -level 51 instead of -level 153. |
| 0 | 2003 | | if (double.TryParse(level, CultureInfo.InvariantCulture, out double hevcLevel)) |
| | 2004 | | { |
| 0 | 2005 | | param += " -level " + (hevcLevel / 3); |
| | 2006 | | } |
| | 2007 | | } |
| 0 | 2008 | | else if (string.Equals(videoEncoder, "av1_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 2009 | | || string.Equals(videoEncoder, "libsvtav1", StringComparison.OrdinalIgnoreCase)) |
| | 2010 | | { |
| | 2011 | | // libsvtav1 and av1_qsv use -level 60 instead of -level 16 |
| | 2012 | | // https://aomedia.org/av1/specification/annex-a/ |
| 0 | 2013 | | if (int.TryParse(level, NumberStyles.Any, CultureInfo.InvariantCulture, out int av1Level)) |
| | 2014 | | { |
| 0 | 2015 | | var x = 2 + (av1Level >> 2); |
| 0 | 2016 | | var y = av1Level & 3; |
| 0 | 2017 | | var res = (x * 10) + y; |
| 0 | 2018 | | param += " -level " + res; |
| | 2019 | | } |
| | 2020 | | } |
| 0 | 2021 | | else if (string.Equals(videoEncoder, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 2022 | | || string.Equals(videoEncoder, "hevc_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 2023 | | || string.Equals(videoEncoder, "av1_amf", StringComparison.OrdinalIgnoreCase)) |
| | 2024 | | { |
| 0 | 2025 | | param += " -level " + level; |
| | 2026 | | } |
| 0 | 2027 | | else if (string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 2028 | | || string.Equals(videoEncoder, "hevc_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 2029 | | || string.Equals(videoEncoder, "av1_nvenc", StringComparison.OrdinalIgnoreCase)) |
| | 2030 | | { |
| | 2031 | | // level option may cause NVENC to fail. |
| | 2032 | | // NVENC cannot adjust the given level, just throw an error. |
| | 2033 | | } |
| 0 | 2034 | | else if (string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 2035 | | || string.Equals(videoEncoder, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 2036 | | || string.Equals(videoEncoder, "av1_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 2037 | | { |
| | 2038 | | // level option may cause corrupted frames on AMD VAAPI. |
| 0 | 2039 | | if (_mediaEncoder.IsVaapiDeviceInteliHD || _mediaEncoder.IsVaapiDeviceInteli965) |
| | 2040 | | { |
| 0 | 2041 | | param += " -level " + level; |
| | 2042 | | } |
| | 2043 | | } |
| 0 | 2044 | | else if (string.Equals(videoEncoder, "h264_rkmpp", StringComparison.OrdinalIgnoreCase) |
| 0 | 2045 | | || string.Equals(videoEncoder, "hevc_rkmpp", StringComparison.OrdinalIgnoreCase)) |
| | 2046 | | { |
| 0 | 2047 | | param += " -level " + level; |
| | 2048 | | } |
| 0 | 2049 | | else if (!string.Equals(videoEncoder, "libx265", StringComparison.OrdinalIgnoreCase)) |
| | 2050 | | { |
| 0 | 2051 | | param += " -level " + level; |
| | 2052 | | } |
| | 2053 | | } |
| | 2054 | |
|
| 0 | 2055 | | if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase)) |
| | 2056 | | { |
| 0 | 2057 | | param += " -x264opts:0 subme=0:me_range=16:rc_lookahead=10:me=hex:open_gop=0"; |
| | 2058 | | } |
| | 2059 | |
|
| 0 | 2060 | | if (string.Equals(videoEncoder, "libx265", StringComparison.OrdinalIgnoreCase)) |
| | 2061 | | { |
| | 2062 | | // libx265 only accept level option in -x265-params. |
| | 2063 | | // level option may cause libx265 to fail. |
| | 2064 | | // libx265 cannot adjust the given level, just throw an error. |
| 0 | 2065 | | param += " -x265-params:0 no-scenecut=1:no-open-gop=1:no-info=1"; |
| | 2066 | |
|
| 0 | 2067 | | if (encodingOptions.EncoderPreset < EncoderPreset.ultrafast) |
| | 2068 | | { |
| | 2069 | | // The following params are slower than the ultrafast preset, don't use when ultrafast is selected. |
| 0 | 2070 | | param += ":subme=3:merange=25:rc-lookahead=10:me=star:ctu=32:max-tu-size=32:min-cu-size=16:rskip=2:r |
| | 2071 | | } |
| | 2072 | | } |
| | 2073 | |
|
| 0 | 2074 | | if (string.Equals(videoEncoder, "libsvtav1", StringComparison.OrdinalIgnoreCase) |
| 0 | 2075 | | && _mediaEncoder.EncoderVersion >= _minFFmpegSvtAv1Params) |
| | 2076 | | { |
| 0 | 2077 | | param += " -svtav1-params:0 rc=1:tune=0:film-grain=0:enable-overlays=1:enable-tf=0"; |
| | 2078 | | } |
| | 2079 | |
|
| | 2080 | | /* Access unit too large: 8192 < 20880 error */ |
| 0 | 2081 | | if ((string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) || |
| 0 | 2082 | | string.Equals(videoEncoder, "hevc_vaapi", StringComparison.OrdinalIgnoreCase)) && |
| 0 | 2083 | | _mediaEncoder.EncoderVersion >= _minFFmpegVaapiH26xEncA53CcSei) |
| | 2084 | | { |
| 0 | 2085 | | param += " -sei -a53_cc"; |
| | 2086 | | } |
| | 2087 | |
|
| 0 | 2088 | | return param; |
| | 2089 | | } |
| | 2090 | |
|
| | 2091 | | public bool CanStreamCopyVideo(EncodingJobInfo state, MediaStream videoStream) |
| | 2092 | | { |
| 0 | 2093 | | var request = state.BaseRequest; |
| | 2094 | |
|
| 0 | 2095 | | if (!request.AllowVideoStreamCopy) |
| | 2096 | | { |
| 0 | 2097 | | return false; |
| | 2098 | | } |
| | 2099 | |
|
| 0 | 2100 | | if (videoStream.IsInterlaced |
| 0 | 2101 | | && state.DeInterlace(videoStream.Codec, false)) |
| | 2102 | | { |
| 0 | 2103 | | return false; |
| | 2104 | | } |
| | 2105 | |
|
| 0 | 2106 | | if (videoStream.IsAnamorphic ?? false) |
| | 2107 | | { |
| 0 | 2108 | | if (request.RequireNonAnamorphic) |
| | 2109 | | { |
| 0 | 2110 | | return false; |
| | 2111 | | } |
| | 2112 | | } |
| | 2113 | |
|
| | 2114 | | // Can't stream copy if we're burning in subtitles |
| 0 | 2115 | | if (request.SubtitleStreamIndex.HasValue |
| 0 | 2116 | | && request.SubtitleStreamIndex.Value >= 0 |
| 0 | 2117 | | && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode) |
| | 2118 | | { |
| 0 | 2119 | | return false; |
| | 2120 | | } |
| | 2121 | |
|
| 0 | 2122 | | if (string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 2123 | | && videoStream.IsAVC.HasValue |
| 0 | 2124 | | && !videoStream.IsAVC.Value |
| 0 | 2125 | | && request.RequireAvc) |
| | 2126 | | { |
| 0 | 2127 | | return false; |
| | 2128 | | } |
| | 2129 | |
|
| | 2130 | | // Source and target codecs must match |
| 0 | 2131 | | if (string.IsNullOrEmpty(videoStream.Codec) |
| 0 | 2132 | | || (state.SupportedVideoCodecs.Length != 0 |
| 0 | 2133 | | && !state.SupportedVideoCodecs.Contains(videoStream.Codec, StringComparison.OrdinalIgnoreCase))) |
| | 2134 | | { |
| 0 | 2135 | | return false; |
| | 2136 | | } |
| | 2137 | |
|
| 0 | 2138 | | var requestedProfiles = state.GetRequestedProfiles(videoStream.Codec); |
| | 2139 | |
|
| | 2140 | | // If client is requesting a specific video profile, it must match the source |
| 0 | 2141 | | if (requestedProfiles.Length > 0) |
| | 2142 | | { |
| 0 | 2143 | | if (string.IsNullOrEmpty(videoStream.Profile)) |
| | 2144 | | { |
| | 2145 | | // return false; |
| | 2146 | | } |
| | 2147 | |
|
| 0 | 2148 | | var requestedProfile = requestedProfiles[0]; |
| | 2149 | | // strip spaces because they may be stripped out on the query string as well |
| 0 | 2150 | | if (!string.IsNullOrEmpty(videoStream.Profile) |
| 0 | 2151 | | && !requestedProfiles.Contains(videoStream.Profile.Replace(" ", string.Empty, StringComparison.Ordin |
| | 2152 | | { |
| 0 | 2153 | | var currentScore = GetVideoProfileScore(videoStream.Codec, videoStream.Profile); |
| 0 | 2154 | | var requestedScore = GetVideoProfileScore(videoStream.Codec, requestedProfile); |
| | 2155 | |
|
| 0 | 2156 | | if (currentScore == -1 || currentScore > requestedScore) |
| | 2157 | | { |
| 0 | 2158 | | return false; |
| | 2159 | | } |
| | 2160 | | } |
| | 2161 | | } |
| | 2162 | |
|
| 0 | 2163 | | var requestedRangeTypes = state.GetRequestedRangeTypes(videoStream.Codec); |
| 0 | 2164 | | if (requestedRangeTypes.Length > 0) |
| | 2165 | | { |
| 0 | 2166 | | if (videoStream.VideoRangeType == VideoRangeType.Unknown) |
| | 2167 | | { |
| 0 | 2168 | | return false; |
| | 2169 | | } |
| | 2170 | |
|
| | 2171 | | // DOVIWithHDR10 should be compatible with HDR10 supporting players. Same goes with HLG and of course SD |
| | 2172 | |
|
| 0 | 2173 | | var requestHasHDR10 = requestedRangeTypes.Contains(VideoRangeType.HDR10.ToString(), StringComparison.Ord |
| 0 | 2174 | | var requestHasHLG = requestedRangeTypes.Contains(VideoRangeType.HLG.ToString(), StringComparison.Ordinal |
| 0 | 2175 | | var requestHasSDR = requestedRangeTypes.Contains(VideoRangeType.SDR.ToString(), StringComparison.Ordinal |
| | 2176 | |
|
| 0 | 2177 | | if (!requestedRangeTypes.Contains(videoStream.VideoRangeType.ToString(), StringComparison.OrdinalIgnoreC |
| 0 | 2178 | | && !((requestHasHDR10 && videoStream.VideoRangeType == VideoRangeType.DOVIWithHDR10) |
| 0 | 2179 | | || (requestHasHLG && videoStream.VideoRangeType == VideoRangeType.DOVIWithHLG) |
| 0 | 2180 | | || (requestHasSDR && videoStream.VideoRangeType == VideoRangeType.DOVIWithSDR))) |
| | 2181 | | { |
| 0 | 2182 | | return false; |
| | 2183 | | } |
| | 2184 | | } |
| | 2185 | |
|
| | 2186 | | // Video width must fall within requested value |
| 0 | 2187 | | if (request.MaxWidth.HasValue |
| 0 | 2188 | | && (!videoStream.Width.HasValue || videoStream.Width.Value > request.MaxWidth.Value)) |
| | 2189 | | { |
| 0 | 2190 | | return false; |
| | 2191 | | } |
| | 2192 | |
|
| | 2193 | | // Video height must fall within requested value |
| 0 | 2194 | | if (request.MaxHeight.HasValue |
| 0 | 2195 | | && (!videoStream.Height.HasValue || videoStream.Height.Value > request.MaxHeight.Value)) |
| | 2196 | | { |
| 0 | 2197 | | return false; |
| | 2198 | | } |
| | 2199 | |
|
| | 2200 | | // Video framerate must fall within requested value |
| 0 | 2201 | | var requestedFramerate = request.MaxFramerate ?? request.Framerate; |
| 0 | 2202 | | if (requestedFramerate.HasValue) |
| | 2203 | | { |
| 0 | 2204 | | var videoFrameRate = videoStream.ReferenceFrameRate; |
| | 2205 | |
|
| | 2206 | | // Add a little tolerance to the framerate check because some videos might record a framerate |
| | 2207 | | // that is slightly greater than the intended framerate, but the device can still play it correctly. |
| | 2208 | | // 0.05 fps tolerance should be safe enough. |
| 0 | 2209 | | if (!videoFrameRate.HasValue || videoFrameRate.Value > requestedFramerate.Value + 0.05f) |
| | 2210 | | { |
| 0 | 2211 | | return false; |
| | 2212 | | } |
| | 2213 | | } |
| | 2214 | |
|
| | 2215 | | // Video bitrate must fall within requested value |
| 0 | 2216 | | if (request.VideoBitRate.HasValue |
| 0 | 2217 | | && (!videoStream.BitRate.HasValue || videoStream.BitRate.Value > request.VideoBitRate.Value)) |
| | 2218 | | { |
| | 2219 | | // For LiveTV that has no bitrate, let's try copy if other conditions are met |
| 0 | 2220 | | if (string.IsNullOrWhiteSpace(request.LiveStreamId) || videoStream.BitRate.HasValue) |
| | 2221 | | { |
| 0 | 2222 | | return false; |
| | 2223 | | } |
| | 2224 | | } |
| | 2225 | |
|
| 0 | 2226 | | var maxBitDepth = state.GetRequestedVideoBitDepth(videoStream.Codec); |
| 0 | 2227 | | if (maxBitDepth.HasValue) |
| | 2228 | | { |
| 0 | 2229 | | if (videoStream.BitDepth.HasValue && videoStream.BitDepth.Value > maxBitDepth.Value) |
| | 2230 | | { |
| 0 | 2231 | | return false; |
| | 2232 | | } |
| | 2233 | | } |
| | 2234 | |
|
| 0 | 2235 | | var maxRefFrames = state.GetRequestedMaxRefFrames(videoStream.Codec); |
| 0 | 2236 | | if (maxRefFrames.HasValue |
| 0 | 2237 | | && videoStream.RefFrames.HasValue && videoStream.RefFrames.Value > maxRefFrames.Value) |
| | 2238 | | { |
| 0 | 2239 | | return false; |
| | 2240 | | } |
| | 2241 | |
|
| | 2242 | | // If a specific level was requested, the source must match or be less than |
| 0 | 2243 | | var level = state.GetRequestedLevel(videoStream.Codec); |
| 0 | 2244 | | if (double.TryParse(level, CultureInfo.InvariantCulture, out var requestLevel)) |
| | 2245 | | { |
| 0 | 2246 | | if (!videoStream.Level.HasValue) |
| | 2247 | | { |
| | 2248 | | // return false; |
| | 2249 | | } |
| | 2250 | |
|
| 0 | 2251 | | if (videoStream.Level.HasValue && videoStream.Level.Value > requestLevel) |
| | 2252 | | { |
| 0 | 2253 | | return false; |
| | 2254 | | } |
| | 2255 | | } |
| | 2256 | |
|
| 0 | 2257 | | if (string.Equals(state.InputContainer, "avi", StringComparison.OrdinalIgnoreCase) |
| 0 | 2258 | | && string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase) |
| 0 | 2259 | | && !(videoStream.IsAVC ?? false)) |
| | 2260 | | { |
| | 2261 | | // see Coach S01E01 - Kelly and the Professor(0).avi |
| 0 | 2262 | | return false; |
| | 2263 | | } |
| | 2264 | |
|
| 0 | 2265 | | return true; |
| | 2266 | | } |
| | 2267 | |
|
| | 2268 | | public bool CanStreamCopyAudio(EncodingJobInfo state, MediaStream audioStream, IEnumerable<string> supportedAudi |
| | 2269 | | { |
| 0 | 2270 | | var request = state.BaseRequest; |
| | 2271 | |
|
| 0 | 2272 | | if (!request.AllowAudioStreamCopy) |
| | 2273 | | { |
| 0 | 2274 | | return false; |
| | 2275 | | } |
| | 2276 | |
|
| 0 | 2277 | | var maxBitDepth = state.GetRequestedAudioBitDepth(audioStream.Codec); |
| 0 | 2278 | | if (maxBitDepth.HasValue |
| 0 | 2279 | | && audioStream.BitDepth.HasValue |
| 0 | 2280 | | && audioStream.BitDepth.Value > maxBitDepth.Value) |
| | 2281 | | { |
| 0 | 2282 | | return false; |
| | 2283 | | } |
| | 2284 | |
|
| | 2285 | | // Source and target codecs must match |
| 0 | 2286 | | if (string.IsNullOrEmpty(audioStream.Codec) |
| 0 | 2287 | | || !supportedAudioCodecs.Contains(audioStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 2288 | | { |
| 0 | 2289 | | return false; |
| | 2290 | | } |
| | 2291 | |
|
| | 2292 | | // Channels must fall within requested value |
| 0 | 2293 | | var channels = state.GetRequestedAudioChannels(audioStream.Codec); |
| 0 | 2294 | | if (channels.HasValue) |
| | 2295 | | { |
| 0 | 2296 | | if (!audioStream.Channels.HasValue || audioStream.Channels.Value <= 0) |
| | 2297 | | { |
| 0 | 2298 | | return false; |
| | 2299 | | } |
| | 2300 | |
|
| 0 | 2301 | | if (audioStream.Channels.Value > channels.Value) |
| | 2302 | | { |
| 0 | 2303 | | return false; |
| | 2304 | | } |
| | 2305 | | } |
| | 2306 | |
|
| | 2307 | | // Sample rate must fall within requested value |
| 0 | 2308 | | if (request.AudioSampleRate.HasValue) |
| | 2309 | | { |
| 0 | 2310 | | if (!audioStream.SampleRate.HasValue || audioStream.SampleRate.Value <= 0) |
| | 2311 | | { |
| 0 | 2312 | | return false; |
| | 2313 | | } |
| | 2314 | |
|
| 0 | 2315 | | if (audioStream.SampleRate.Value > request.AudioSampleRate.Value) |
| | 2316 | | { |
| 0 | 2317 | | return false; |
| | 2318 | | } |
| | 2319 | | } |
| | 2320 | |
|
| | 2321 | | // Audio bitrate must fall within requested value |
| 0 | 2322 | | if (request.AudioBitRate.HasValue |
| 0 | 2323 | | && audioStream.BitRate.HasValue |
| 0 | 2324 | | && audioStream.BitRate.Value > request.AudioBitRate.Value) |
| | 2325 | | { |
| 0 | 2326 | | return false; |
| | 2327 | | } |
| | 2328 | |
|
| 0 | 2329 | | return request.EnableAutoStreamCopy; |
| | 2330 | | } |
| | 2331 | |
|
| | 2332 | | public int GetVideoBitrateParamValue(BaseEncodingJobOptions request, MediaStream videoStream, string outputVideo |
| | 2333 | | { |
| 0 | 2334 | | var bitrate = request.VideoBitRate; |
| | 2335 | |
|
| 0 | 2336 | | if (videoStream is not null) |
| | 2337 | | { |
| 0 | 2338 | | var isUpscaling = request.Height.HasValue |
| 0 | 2339 | | && videoStream.Height.HasValue |
| 0 | 2340 | | && request.Height.Value > videoStream.Height.Value |
| 0 | 2341 | | && request.Width.HasValue |
| 0 | 2342 | | && videoStream.Width.HasValue |
| 0 | 2343 | | && request.Width.Value > videoStream.Width.Value; |
| | 2344 | |
|
| | 2345 | | // Don't allow bitrate increases unless upscaling |
| 0 | 2346 | | if (!isUpscaling && bitrate.HasValue && videoStream.BitRate.HasValue) |
| | 2347 | | { |
| 0 | 2348 | | bitrate = GetMinBitrate(videoStream.BitRate.Value, bitrate.Value); |
| | 2349 | | } |
| | 2350 | |
|
| 0 | 2351 | | if (bitrate.HasValue) |
| | 2352 | | { |
| 0 | 2353 | | var inputVideoCodec = videoStream.Codec; |
| 0 | 2354 | | bitrate = ScaleBitrate(bitrate.Value, inputVideoCodec, outputVideoCodec); |
| | 2355 | |
|
| | 2356 | | // If a max bitrate was requested, don't let the scaled bitrate exceed it |
| 0 | 2357 | | if (request.VideoBitRate.HasValue) |
| | 2358 | | { |
| 0 | 2359 | | bitrate = Math.Min(bitrate.Value, request.VideoBitRate.Value); |
| | 2360 | | } |
| | 2361 | | } |
| | 2362 | | } |
| | 2363 | |
|
| | 2364 | | // Cap the max target bitrate to intMax/2 to satisfy the bufsize=bitrate*2. |
| 0 | 2365 | | return Math.Min(bitrate ?? 0, int.MaxValue / 2); |
| | 2366 | | } |
| | 2367 | |
|
| | 2368 | | private int GetMinBitrate(int sourceBitrate, int requestedBitrate) |
| | 2369 | | { |
| | 2370 | | // these values were chosen from testing to improve low bitrate streams |
| 0 | 2371 | | if (sourceBitrate <= 2000000) |
| | 2372 | | { |
| 0 | 2373 | | sourceBitrate = Convert.ToInt32(sourceBitrate * 2.5); |
| | 2374 | | } |
| 0 | 2375 | | else if (sourceBitrate <= 3000000) |
| | 2376 | | { |
| 0 | 2377 | | sourceBitrate *= 2; |
| | 2378 | | } |
| | 2379 | |
|
| 0 | 2380 | | var bitrate = Math.Min(sourceBitrate, requestedBitrate); |
| | 2381 | |
|
| 0 | 2382 | | return bitrate; |
| | 2383 | | } |
| | 2384 | |
|
| | 2385 | | private static double GetVideoBitrateScaleFactor(string codec) |
| | 2386 | | { |
| | 2387 | | // hevc & vp9 - 40% more efficient than h.264 |
| 0 | 2388 | | if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase) |
| 0 | 2389 | | || string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 2390 | | || string.Equals(codec, "vp9", StringComparison.OrdinalIgnoreCase)) |
| | 2391 | | { |
| 0 | 2392 | | return .6; |
| | 2393 | | } |
| | 2394 | |
|
| | 2395 | | // av1 - 50% more efficient than h.264 |
| 0 | 2396 | | if (string.Equals(codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 2397 | | { |
| 0 | 2398 | | return .5; |
| | 2399 | | } |
| | 2400 | |
|
| 0 | 2401 | | return 1; |
| | 2402 | | } |
| | 2403 | |
|
| | 2404 | | public static int ScaleBitrate(int bitrate, string inputVideoCodec, string outputVideoCodec) |
| | 2405 | | { |
| 0 | 2406 | | var inputScaleFactor = GetVideoBitrateScaleFactor(inputVideoCodec); |
| 0 | 2407 | | var outputScaleFactor = GetVideoBitrateScaleFactor(outputVideoCodec); |
| | 2408 | |
|
| | 2409 | | // Don't scale the real bitrate lower than the requested bitrate |
| 0 | 2410 | | var scaleFactor = Math.Max(outputScaleFactor / inputScaleFactor, 1); |
| | 2411 | |
|
| 0 | 2412 | | if (bitrate <= 500000) |
| | 2413 | | { |
| 0 | 2414 | | scaleFactor = Math.Max(scaleFactor, 4); |
| | 2415 | | } |
| 0 | 2416 | | else if (bitrate <= 1000000) |
| | 2417 | | { |
| 0 | 2418 | | scaleFactor = Math.Max(scaleFactor, 3); |
| | 2419 | | } |
| 0 | 2420 | | else if (bitrate <= 2000000) |
| | 2421 | | { |
| 0 | 2422 | | scaleFactor = Math.Max(scaleFactor, 2.5); |
| | 2423 | | } |
| 0 | 2424 | | else if (bitrate <= 3000000) |
| | 2425 | | { |
| 0 | 2426 | | scaleFactor = Math.Max(scaleFactor, 2); |
| | 2427 | | } |
| 0 | 2428 | | else if (bitrate >= 30000000) |
| | 2429 | | { |
| | 2430 | | // Don't scale beyond 30Mbps, it is hardly visually noticeable for most codecs with our prefer speed enc |
| | 2431 | | // and will cause extremely high bitrate to be used for av1->h264 transcoding that will overload clients |
| 0 | 2432 | | scaleFactor = 1; |
| | 2433 | | } |
| | 2434 | |
|
| 0 | 2435 | | return Convert.ToInt32(scaleFactor * bitrate); |
| | 2436 | | } |
| | 2437 | |
|
| | 2438 | | public int? GetAudioBitrateParam(BaseEncodingJobOptions request, MediaStream audioStream, int? outputAudioChanne |
| | 2439 | | { |
| 0 | 2440 | | return GetAudioBitrateParam(request.AudioBitRate, request.AudioCodec, audioStream, outputAudioChannels); |
| | 2441 | | } |
| | 2442 | |
|
| | 2443 | | public int? GetAudioBitrateParam(int? audioBitRate, string audioCodec, MediaStream audioStream, int? outputAudio |
| | 2444 | | { |
| 0 | 2445 | | if (audioStream is null) |
| | 2446 | | { |
| 0 | 2447 | | return null; |
| | 2448 | | } |
| | 2449 | |
|
| 0 | 2450 | | var inputChannels = audioStream.Channels ?? 0; |
| 0 | 2451 | | var outputChannels = outputAudioChannels ?? 0; |
| 0 | 2452 | | var bitrate = audioBitRate ?? int.MaxValue; |
| | 2453 | |
|
| 0 | 2454 | | if (string.IsNullOrEmpty(audioCodec) |
| 0 | 2455 | | || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase) |
| 0 | 2456 | | || string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase) |
| 0 | 2457 | | || string.Equals(audioCodec, "opus", StringComparison.OrdinalIgnoreCase) |
| 0 | 2458 | | || string.Equals(audioCodec, "vorbis", StringComparison.OrdinalIgnoreCase) |
| 0 | 2459 | | || string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase) |
| 0 | 2460 | | || string.Equals(audioCodec, "eac3", StringComparison.OrdinalIgnoreCase)) |
| | 2461 | | { |
| 0 | 2462 | | return (inputChannels, outputChannels) switch |
| 0 | 2463 | | { |
| 0 | 2464 | | (>= 6, >= 6 or 0) => Math.Min(640000, bitrate), |
| 0 | 2465 | | (> 0, > 0) => Math.Min(outputChannels * 128000, bitrate), |
| 0 | 2466 | | (> 0, _) => Math.Min(inputChannels * 128000, bitrate), |
| 0 | 2467 | | (_, _) => Math.Min(384000, bitrate) |
| 0 | 2468 | | }; |
| | 2469 | | } |
| | 2470 | |
|
| 0 | 2471 | | if (string.Equals(audioCodec, "dts", StringComparison.OrdinalIgnoreCase) |
| 0 | 2472 | | || string.Equals(audioCodec, "dca", StringComparison.OrdinalIgnoreCase)) |
| | 2473 | | { |
| 0 | 2474 | | return (inputChannels, outputChannels) switch |
| 0 | 2475 | | { |
| 0 | 2476 | | (>= 6, >= 6 or 0) => Math.Min(768000, bitrate), |
| 0 | 2477 | | (> 0, > 0) => Math.Min(outputChannels * 136000, bitrate), |
| 0 | 2478 | | (> 0, _) => Math.Min(inputChannels * 136000, bitrate), |
| 0 | 2479 | | (_, _) => Math.Min(672000, bitrate) |
| 0 | 2480 | | }; |
| | 2481 | | } |
| | 2482 | |
|
| | 2483 | | // Empty bitrate area is not allow on iOS |
| | 2484 | | // Default audio bitrate to 128K per channel if we don't have codec specific defaults |
| | 2485 | | // https://ffmpeg.org/ffmpeg-codecs.html#toc-Codec-Options |
| 0 | 2486 | | return 128000 * (outputAudioChannels ?? audioStream.Channels ?? 2); |
| | 2487 | | } |
| | 2488 | |
|
| | 2489 | | public string GetAudioVbrModeParam(string encoder, int bitrate, int channels) |
| | 2490 | | { |
| 0 | 2491 | | var bitratePerChannel = bitrate / Math.Max(channels, 1); |
| 0 | 2492 | | if (string.Equals(encoder, "libfdk_aac", StringComparison.OrdinalIgnoreCase)) |
| | 2493 | | { |
| 0 | 2494 | | return " -vbr:a " + bitratePerChannel switch |
| 0 | 2495 | | { |
| 0 | 2496 | | < 32000 => "1", |
| 0 | 2497 | | < 48000 => "2", |
| 0 | 2498 | | < 64000 => "3", |
| 0 | 2499 | | < 96000 => "4", |
| 0 | 2500 | | _ => "5" |
| 0 | 2501 | | }; |
| | 2502 | | } |
| | 2503 | |
|
| 0 | 2504 | | if (string.Equals(encoder, "libmp3lame", StringComparison.OrdinalIgnoreCase)) |
| | 2505 | | { |
| | 2506 | | // lame's VBR is only good for a certain bitrate range |
| | 2507 | | // For very low and very high bitrate, use abr mode |
| 0 | 2508 | | if (bitratePerChannel is < 122500 and > 48000) |
| | 2509 | | { |
| 0 | 2510 | | return " -qscale:a " + bitratePerChannel switch |
| 0 | 2511 | | { |
| 0 | 2512 | | < 64000 => "6", |
| 0 | 2513 | | < 88000 => "4", |
| 0 | 2514 | | < 112000 => "2", |
| 0 | 2515 | | _ => "0" |
| 0 | 2516 | | }; |
| | 2517 | | } |
| | 2518 | |
|
| 0 | 2519 | | return " -abr:a 1" + " -b:a " + bitrate; |
| | 2520 | | } |
| | 2521 | |
|
| 0 | 2522 | | if (string.Equals(encoder, "aac_at", StringComparison.OrdinalIgnoreCase)) |
| | 2523 | | { |
| | 2524 | | // aac_at's CVBR mode |
| 0 | 2525 | | return " -aac_at_mode:a 2" + " -b:a " + bitrate; |
| | 2526 | | } |
| | 2527 | |
|
| 0 | 2528 | | if (string.Equals(encoder, "libvorbis", StringComparison.OrdinalIgnoreCase)) |
| | 2529 | | { |
| 0 | 2530 | | return " -qscale:a " + bitratePerChannel switch |
| 0 | 2531 | | { |
| 0 | 2532 | | < 40000 => "0", |
| 0 | 2533 | | < 56000 => "2", |
| 0 | 2534 | | < 80000 => "4", |
| 0 | 2535 | | < 112000 => "6", |
| 0 | 2536 | | _ => "8" |
| 0 | 2537 | | }; |
| | 2538 | | } |
| | 2539 | |
|
| 0 | 2540 | | return null; |
| | 2541 | | } |
| | 2542 | |
|
| | 2543 | | public string GetAudioFilterParam(EncodingJobInfo state, EncodingOptions encodingOptions) |
| | 2544 | | { |
| 0 | 2545 | | var channels = state.OutputAudioChannels; |
| | 2546 | |
|
| 0 | 2547 | | var filters = new List<string>(); |
| | 2548 | |
|
| 0 | 2549 | | if (channels is 2 && state.AudioStream?.Channels is > 2) |
| | 2550 | | { |
| 0 | 2551 | | var hasDownMixFilter = DownMixAlgorithmsHelper.AlgorithmFilterStrings.TryGetValue((encodingOptions.DownM |
| 0 | 2552 | | if (hasDownMixFilter) |
| | 2553 | | { |
| 0 | 2554 | | filters.Add(downMixFilterString); |
| | 2555 | | } |
| | 2556 | |
|
| 0 | 2557 | | if (!encodingOptions.DownMixAudioBoost.Equals(1)) |
| | 2558 | | { |
| 0 | 2559 | | filters.Add("volume=" + encodingOptions.DownMixAudioBoost.ToString(CultureInfo.InvariantCulture)); |
| | 2560 | | } |
| | 2561 | | } |
| | 2562 | |
|
| 0 | 2563 | | var isCopyingTimestamps = state.CopyTimestamps || state.TranscodingType != TranscodingJobType.Progressive; |
| 0 | 2564 | | if (state.SubtitleStream is not null && state.SubtitleStream.IsTextSubtitleStream && ShouldEncodeSubtitle(st |
| | 2565 | | { |
| 0 | 2566 | | var seconds = TimeSpan.FromTicks(state.StartTimeTicks ?? 0).TotalSeconds; |
| | 2567 | |
|
| 0 | 2568 | | filters.Add( |
| 0 | 2569 | | string.Format( |
| 0 | 2570 | | CultureInfo.InvariantCulture, |
| 0 | 2571 | | "asetpts=PTS-{0}/TB", |
| 0 | 2572 | | Math.Round(seconds))); |
| | 2573 | | } |
| | 2574 | |
|
| 0 | 2575 | | if (filters.Count > 0) |
| | 2576 | | { |
| 0 | 2577 | | return " -af \"" + string.Join(',', filters) + "\""; |
| | 2578 | | } |
| | 2579 | |
|
| 0 | 2580 | | return string.Empty; |
| | 2581 | | } |
| | 2582 | |
|
| | 2583 | | /// <summary> |
| | 2584 | | /// Gets the number of audio channels to specify on the command line. |
| | 2585 | | /// </summary> |
| | 2586 | | /// <param name="state">The state.</param> |
| | 2587 | | /// <param name="audioStream">The audio stream.</param> |
| | 2588 | | /// <param name="outputAudioCodec">The output audio codec.</param> |
| | 2589 | | /// <returns>System.Nullable{System.Int32}.</returns> |
| | 2590 | | public int? GetNumAudioChannelsParam(EncodingJobInfo state, MediaStream audioStream, string outputAudioCodec) |
| | 2591 | | { |
| 0 | 2592 | | if (audioStream is null) |
| | 2593 | | { |
| 0 | 2594 | | return null; |
| | 2595 | | } |
| | 2596 | |
|
| 0 | 2597 | | var request = state.BaseRequest; |
| | 2598 | |
|
| 0 | 2599 | | var codec = outputAudioCodec ?? string.Empty; |
| | 2600 | |
|
| 0 | 2601 | | int? resultChannels = state.GetRequestedAudioChannels(codec); |
| | 2602 | |
|
| 0 | 2603 | | var inputChannels = audioStream.Channels; |
| | 2604 | |
|
| 0 | 2605 | | if (inputChannels > 0) |
| | 2606 | | { |
| 0 | 2607 | | resultChannels = inputChannels < resultChannels ? inputChannels : resultChannels ?? inputChannels; |
| | 2608 | | } |
| | 2609 | |
|
| 0 | 2610 | | var isTranscodingAudio = !IsCopyCodec(codec); |
| | 2611 | |
|
| 0 | 2612 | | if (isTranscodingAudio) |
| | 2613 | | { |
| 0 | 2614 | | var audioEncoder = GetAudioEncoder(state); |
| 0 | 2615 | | if (!_audioTranscodeChannelLookup.TryGetValue(audioEncoder, out var transcoderChannelLimit)) |
| | 2616 | | { |
| | 2617 | | // Set default max transcoding channels to 8 to prevent encoding errors due to asking for too many c |
| 0 | 2618 | | transcoderChannelLimit = 8; |
| | 2619 | | } |
| | 2620 | |
|
| | 2621 | | // Set resultChannels to minimum between resultChannels, TranscodingMaxAudioChannels, transcoderChannelL |
| 0 | 2622 | | resultChannels = transcoderChannelLimit < resultChannels ? transcoderChannelLimit : resultChannels ?? tr |
| | 2623 | |
|
| 0 | 2624 | | if (request.TranscodingMaxAudioChannels < resultChannels) |
| | 2625 | | { |
| 0 | 2626 | | resultChannels = request.TranscodingMaxAudioChannels; |
| | 2627 | | } |
| | 2628 | |
|
| | 2629 | | // Avoid transcoding to audio channels other than 1ch, 2ch, 6ch (5.1 layout) and 8ch (7.1 layout). |
| | 2630 | | // https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_d |
| 0 | 2631 | | if (state.TranscodingType != TranscodingJobType.Progressive |
| 0 | 2632 | | && ((resultChannels > 2 && resultChannels < 6) || resultChannels == 7)) |
| | 2633 | | { |
| | 2634 | | // We can let FFMpeg supply an extra LFE channel for 5ch and 7ch to make them 5.1 and 7.1 |
| 0 | 2635 | | if (resultChannels == 5) |
| | 2636 | | { |
| 0 | 2637 | | resultChannels = 6; |
| | 2638 | | } |
| 0 | 2639 | | else if (resultChannels == 7) |
| | 2640 | | { |
| 0 | 2641 | | resultChannels = 8; |
| | 2642 | | } |
| | 2643 | | else |
| | 2644 | | { |
| | 2645 | | // For other weird layout, just downmix to stereo for compatibility |
| 0 | 2646 | | resultChannels = 2; |
| | 2647 | | } |
| | 2648 | | } |
| | 2649 | | } |
| | 2650 | |
|
| 0 | 2651 | | return resultChannels; |
| | 2652 | | } |
| | 2653 | |
|
| | 2654 | | /// <summary> |
| | 2655 | | /// Enforces the resolution limit. |
| | 2656 | | /// </summary> |
| | 2657 | | /// <param name="state">The state.</param> |
| | 2658 | | public void EnforceResolutionLimit(EncodingJobInfo state) |
| | 2659 | | { |
| 0 | 2660 | | var videoRequest = state.BaseRequest; |
| | 2661 | |
|
| | 2662 | | // Switch the incoming params to be ceilings rather than fixed values |
| 0 | 2663 | | videoRequest.MaxWidth = videoRequest.MaxWidth ?? videoRequest.Width; |
| 0 | 2664 | | videoRequest.MaxHeight = videoRequest.MaxHeight ?? videoRequest.Height; |
| | 2665 | |
|
| 0 | 2666 | | videoRequest.Width = null; |
| 0 | 2667 | | videoRequest.Height = null; |
| 0 | 2668 | | } |
| | 2669 | |
|
| | 2670 | | /// <summary> |
| | 2671 | | /// Gets the fast seek command line parameter. |
| | 2672 | | /// </summary> |
| | 2673 | | /// <param name="state">The state.</param> |
| | 2674 | | /// <param name="options">The options.</param> |
| | 2675 | | /// <param name="segmentContainer">Segment Container.</param> |
| | 2676 | | /// <returns>System.String.</returns> |
| | 2677 | | /// <value>The fast seek command line parameter.</value> |
| | 2678 | | public string GetFastSeekCommandLineParameter(EncodingJobInfo state, EncodingOptions options, string segmentCont |
| | 2679 | | { |
| 0 | 2680 | | var time = state.BaseRequest.StartTimeTicks ?? 0; |
| 0 | 2681 | | var maxTime = state.RunTimeTicks ?? 0; |
| 0 | 2682 | | var seekParam = string.Empty; |
| | 2683 | |
|
| 0 | 2684 | | if (time > 0) |
| | 2685 | | { |
| | 2686 | | // For direct streaming/remuxing, we seek at the exact position of the keyframe |
| | 2687 | | // However, ffmpeg will seek to previous keyframe when the exact time is the input |
| | 2688 | | // Workaround this by adding 0.5s offset to the seeking time to get the exact keyframe on most videos. |
| | 2689 | | // This will help subtitle syncing. |
| 0 | 2690 | | var isHlsRemuxing = state.IsVideoRequest && state.TranscodingType is TranscodingJobType.Hls && IsCopyCod |
| 0 | 2691 | | var seekTick = isHlsRemuxing ? time + 5000000L : time; |
| | 2692 | |
|
| | 2693 | | // Seeking beyond EOF makes no sense in transcoding. Clamp the seekTick value to |
| | 2694 | | // [0, RuntimeTicks - 0.5s], so that the muxer gets packets and avoid error codes. |
| 0 | 2695 | | if (maxTime > 0) |
| | 2696 | | { |
| 0 | 2697 | | seekTick = Math.Clamp(seekTick, 0, Math.Max(maxTime - 5000000L, 0)); |
| | 2698 | | } |
| | 2699 | |
|
| 0 | 2700 | | seekParam += string.Format(CultureInfo.InvariantCulture, "-ss {0}", _mediaEncoder.GetTimeParameter(seekT |
| | 2701 | |
|
| 0 | 2702 | | if (state.IsVideoRequest) |
| | 2703 | | { |
| 0 | 2704 | | var outputVideoCodec = GetVideoEncoder(state, options); |
| 0 | 2705 | | var segmentFormat = GetSegmentFileExtension(segmentContainer).TrimStart('.'); |
| | 2706 | |
|
| | 2707 | | // Important: If this is ever re-enabled, make sure not to use it with wtv because it breaks seeking |
| | 2708 | | // Disable -noaccurate_seek on mpegts container due to the timestamps issue on some clients, |
| | 2709 | | // but it's still required for fMP4 container otherwise the audio can't be synced to the video. |
| 0 | 2710 | | if (!string.Equals(state.InputContainer, "wtv", StringComparison.OrdinalIgnoreCase) |
| 0 | 2711 | | && !string.Equals(segmentFormat, "ts", StringComparison.OrdinalIgnoreCase) |
| 0 | 2712 | | && state.TranscodingType != TranscodingJobType.Progressive |
| 0 | 2713 | | && !state.EnableBreakOnNonKeyFrames(outputVideoCodec) |
| 0 | 2714 | | && (state.BaseRequest.StartTimeTicks ?? 0) > 0) |
| | 2715 | | { |
| 0 | 2716 | | seekParam += " -noaccurate_seek"; |
| | 2717 | | } |
| | 2718 | | } |
| | 2719 | | } |
| | 2720 | |
|
| 0 | 2721 | | return seekParam; |
| | 2722 | | } |
| | 2723 | |
|
| | 2724 | | /// <summary> |
| | 2725 | | /// Gets the map args. |
| | 2726 | | /// </summary> |
| | 2727 | | /// <param name="state">The state.</param> |
| | 2728 | | /// <returns>System.String.</returns> |
| | 2729 | | public string GetMapArgs(EncodingJobInfo state) |
| | 2730 | | { |
| | 2731 | | // If we don't have known media info |
| | 2732 | | // If input is video, use -sn to drop subtitles |
| | 2733 | | // Otherwise just return empty |
| 0 | 2734 | | if (state.VideoStream is null && state.AudioStream is null) |
| | 2735 | | { |
| 0 | 2736 | | return state.IsInputVideo ? "-sn" : string.Empty; |
| | 2737 | | } |
| | 2738 | |
|
| | 2739 | | // We have media info, but we don't know the stream index |
| 0 | 2740 | | if (state.VideoStream is not null && state.VideoStream.Index == -1) |
| | 2741 | | { |
| 0 | 2742 | | return "-sn"; |
| | 2743 | | } |
| | 2744 | |
|
| | 2745 | | // We have media info, but we don't know the stream index |
| 0 | 2746 | | if (state.AudioStream is not null && state.AudioStream.Index == -1) |
| | 2747 | | { |
| 0 | 2748 | | return state.IsInputVideo ? "-sn" : string.Empty; |
| | 2749 | | } |
| | 2750 | |
|
| 0 | 2751 | | var args = string.Empty; |
| | 2752 | |
|
| 0 | 2753 | | if (state.VideoStream is not null) |
| | 2754 | | { |
| 0 | 2755 | | int videoStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.VideoStream); |
| | 2756 | |
|
| 0 | 2757 | | args += string.Format( |
| 0 | 2758 | | CultureInfo.InvariantCulture, |
| 0 | 2759 | | "-map 0:{0}", |
| 0 | 2760 | | videoStreamIndex); |
| | 2761 | | } |
| | 2762 | | else |
| | 2763 | | { |
| | 2764 | | // No known video stream |
| 0 | 2765 | | args += "-vn"; |
| | 2766 | | } |
| | 2767 | |
|
| 0 | 2768 | | if (state.AudioStream is not null) |
| | 2769 | | { |
| 0 | 2770 | | int audioStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.AudioStream); |
| 0 | 2771 | | if (state.AudioStream.IsExternal) |
| | 2772 | | { |
| 0 | 2773 | | bool hasExternalGraphicsSubs = state.SubtitleStream is not null |
| 0 | 2774 | | && ShouldEncodeSubtitle(state) |
| 0 | 2775 | | && state.SubtitleStream.IsExternal |
| 0 | 2776 | | && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 2777 | | int externalAudioMapIndex = hasExternalGraphicsSubs ? 2 : 1; |
| | 2778 | |
|
| 0 | 2779 | | args += string.Format( |
| 0 | 2780 | | CultureInfo.InvariantCulture, |
| 0 | 2781 | | " -map {0}:{1}", |
| 0 | 2782 | | externalAudioMapIndex, |
| 0 | 2783 | | audioStreamIndex); |
| | 2784 | | } |
| | 2785 | | else |
| | 2786 | | { |
| 0 | 2787 | | args += string.Format( |
| 0 | 2788 | | CultureInfo.InvariantCulture, |
| 0 | 2789 | | " -map 0:{0}", |
| 0 | 2790 | | audioStreamIndex); |
| | 2791 | | } |
| | 2792 | | } |
| | 2793 | | else |
| | 2794 | | { |
| 0 | 2795 | | args += " -map -0:a"; |
| | 2796 | | } |
| | 2797 | |
|
| 0 | 2798 | | var subtitleMethod = state.SubtitleDeliveryMethod; |
| 0 | 2799 | | if (state.SubtitleStream is null || subtitleMethod == SubtitleDeliveryMethod.Hls) |
| | 2800 | | { |
| 0 | 2801 | | args += " -map -0:s"; |
| | 2802 | | } |
| 0 | 2803 | | else if (subtitleMethod == SubtitleDeliveryMethod.Embed) |
| | 2804 | | { |
| 0 | 2805 | | int subtitleStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.SubtitleStream); |
| | 2806 | |
|
| 0 | 2807 | | args += string.Format( |
| 0 | 2808 | | CultureInfo.InvariantCulture, |
| 0 | 2809 | | " -map 0:{0}", |
| 0 | 2810 | | subtitleStreamIndex); |
| | 2811 | | } |
| 0 | 2812 | | else if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream) |
| | 2813 | | { |
| 0 | 2814 | | int externalSubtitleStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.SubtitleStream); |
| | 2815 | |
|
| 0 | 2816 | | args += string.Format( |
| 0 | 2817 | | CultureInfo.InvariantCulture, |
| 0 | 2818 | | " -map 1:{0} -sn", |
| 0 | 2819 | | externalSubtitleStreamIndex); |
| | 2820 | | } |
| | 2821 | |
|
| 0 | 2822 | | return args; |
| | 2823 | | } |
| | 2824 | |
|
| | 2825 | | /// <summary> |
| | 2826 | | /// Gets the negative map args by filters. |
| | 2827 | | /// </summary> |
| | 2828 | | /// <param name="state">The state.</param> |
| | 2829 | | /// <param name="videoProcessFilters">The videoProcessFilters.</param> |
| | 2830 | | /// <returns>System.String.</returns> |
| | 2831 | | public string GetNegativeMapArgsByFilters(EncodingJobInfo state, string videoProcessFilters) |
| | 2832 | | { |
| 0 | 2833 | | string args = string.Empty; |
| | 2834 | |
|
| | 2835 | | // http://ffmpeg.org/ffmpeg-all.html#toc-Complex-filtergraphs-1 |
| 0 | 2836 | | if (state.VideoStream is not null && videoProcessFilters.Contains("-filter_complex", StringComparison.Ordina |
| | 2837 | | { |
| 0 | 2838 | | int videoStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.VideoStream); |
| | 2839 | |
|
| 0 | 2840 | | args += string.Format( |
| 0 | 2841 | | CultureInfo.InvariantCulture, |
| 0 | 2842 | | "-map -0:{0} ", |
| 0 | 2843 | | videoStreamIndex); |
| | 2844 | | } |
| | 2845 | |
|
| 0 | 2846 | | return args; |
| | 2847 | | } |
| | 2848 | |
|
| | 2849 | | /// <summary> |
| | 2850 | | /// Determines which stream will be used for playback. |
| | 2851 | | /// </summary> |
| | 2852 | | /// <param name="allStream">All stream.</param> |
| | 2853 | | /// <param name="desiredIndex">Index of the desired.</param> |
| | 2854 | | /// <param name="type">The type.</param> |
| | 2855 | | /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param> |
| | 2856 | | /// <returns>MediaStream.</returns> |
| | 2857 | | public MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, b |
| | 2858 | | { |
| 0 | 2859 | | var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList(); |
| | 2860 | |
|
| 0 | 2861 | | if (desiredIndex.HasValue) |
| | 2862 | | { |
| 0 | 2863 | | var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value); |
| | 2864 | |
|
| 0 | 2865 | | if (stream is not null) |
| | 2866 | | { |
| 0 | 2867 | | return stream; |
| | 2868 | | } |
| | 2869 | | } |
| | 2870 | |
|
| 0 | 2871 | | if (returnFirstIfNoIndex && type == MediaStreamType.Audio) |
| | 2872 | | { |
| 0 | 2873 | | return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ?? |
| 0 | 2874 | | streams.FirstOrDefault(); |
| | 2875 | | } |
| | 2876 | |
|
| | 2877 | | // Just return the first one |
| 0 | 2878 | | return returnFirstIfNoIndex ? streams.FirstOrDefault() : null; |
| | 2879 | | } |
| | 2880 | |
|
| | 2881 | | public static (int? Width, int? Height) GetFixedOutputSize( |
| | 2882 | | int? videoWidth, |
| | 2883 | | int? videoHeight, |
| | 2884 | | int? requestedWidth, |
| | 2885 | | int? requestedHeight, |
| | 2886 | | int? requestedMaxWidth, |
| | 2887 | | int? requestedMaxHeight) |
| | 2888 | | { |
| 0 | 2889 | | if (!videoWidth.HasValue && !requestedWidth.HasValue) |
| | 2890 | | { |
| 0 | 2891 | | return (null, null); |
| | 2892 | | } |
| | 2893 | |
|
| 0 | 2894 | | if (!videoHeight.HasValue && !requestedHeight.HasValue) |
| | 2895 | | { |
| 0 | 2896 | | return (null, null); |
| | 2897 | | } |
| | 2898 | |
|
| 0 | 2899 | | int inputWidth = Convert.ToInt32(videoWidth ?? requestedWidth, CultureInfo.InvariantCulture); |
| 0 | 2900 | | int inputHeight = Convert.ToInt32(videoHeight ?? requestedHeight, CultureInfo.InvariantCulture); |
| 0 | 2901 | | int outputWidth = requestedWidth ?? inputWidth; |
| 0 | 2902 | | int outputHeight = requestedHeight ?? inputHeight; |
| | 2903 | |
|
| | 2904 | | // Don't transcode video to bigger than 4k when using HW. |
| 0 | 2905 | | int maximumWidth = Math.Min(requestedMaxWidth ?? outputWidth, 4096); |
| 0 | 2906 | | int maximumHeight = Math.Min(requestedMaxHeight ?? outputHeight, 4096); |
| | 2907 | |
|
| 0 | 2908 | | if (outputWidth > maximumWidth || outputHeight > maximumHeight) |
| | 2909 | | { |
| 0 | 2910 | | var scaleW = (double)maximumWidth / outputWidth; |
| 0 | 2911 | | var scaleH = (double)maximumHeight / outputHeight; |
| 0 | 2912 | | var scale = Math.Min(scaleW, scaleH); |
| 0 | 2913 | | outputWidth = Math.Min(maximumWidth, Convert.ToInt32(outputWidth * scale)); |
| 0 | 2914 | | outputHeight = Math.Min(maximumHeight, Convert.ToInt32(outputHeight * scale)); |
| | 2915 | | } |
| | 2916 | |
|
| 0 | 2917 | | outputWidth = 2 * (outputWidth / 2); |
| 0 | 2918 | | outputHeight = 2 * (outputHeight / 2); |
| | 2919 | |
|
| 0 | 2920 | | return (outputWidth, outputHeight); |
| | 2921 | | } |
| | 2922 | |
|
| | 2923 | | public static bool IsScaleRatioSupported( |
| | 2924 | | int? videoWidth, |
| | 2925 | | int? videoHeight, |
| | 2926 | | int? requestedWidth, |
| | 2927 | | int? requestedHeight, |
| | 2928 | | int? requestedMaxWidth, |
| | 2929 | | int? requestedMaxHeight, |
| | 2930 | | double? maxScaleRatio) |
| | 2931 | | { |
| 0 | 2932 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 2933 | | videoWidth, |
| 0 | 2934 | | videoHeight, |
| 0 | 2935 | | requestedWidth, |
| 0 | 2936 | | requestedHeight, |
| 0 | 2937 | | requestedMaxWidth, |
| 0 | 2938 | | requestedMaxHeight); |
| | 2939 | |
|
| 0 | 2940 | | if (!videoWidth.HasValue |
| 0 | 2941 | | || !videoHeight.HasValue |
| 0 | 2942 | | || !outWidth.HasValue |
| 0 | 2943 | | || !outHeight.HasValue |
| 0 | 2944 | | || !maxScaleRatio.HasValue |
| 0 | 2945 | | || (maxScaleRatio.Value < 1.0f)) |
| | 2946 | | { |
| 0 | 2947 | | return false; |
| | 2948 | | } |
| | 2949 | |
|
| 0 | 2950 | | var minScaleRatio = 1.0f / maxScaleRatio; |
| 0 | 2951 | | var scaleRatioW = (double)outWidth / (double)videoWidth; |
| 0 | 2952 | | var scaleRatioH = (double)outHeight / (double)videoHeight; |
| | 2953 | |
|
| 0 | 2954 | | if (scaleRatioW < minScaleRatio |
| 0 | 2955 | | || scaleRatioW > maxScaleRatio |
| 0 | 2956 | | || scaleRatioH < minScaleRatio |
| 0 | 2957 | | || scaleRatioH > maxScaleRatio) |
| | 2958 | | { |
| 0 | 2959 | | return false; |
| | 2960 | | } |
| | 2961 | |
|
| 0 | 2962 | | return true; |
| | 2963 | | } |
| | 2964 | |
|
| | 2965 | | public static string GetHwScaleFilter( |
| | 2966 | | string hwScalePrefix, |
| | 2967 | | string hwScaleSuffix, |
| | 2968 | | string videoFormat, |
| | 2969 | | bool swapOutputWandH, |
| | 2970 | | int? videoWidth, |
| | 2971 | | int? videoHeight, |
| | 2972 | | int? requestedWidth, |
| | 2973 | | int? requestedHeight, |
| | 2974 | | int? requestedMaxWidth, |
| | 2975 | | int? requestedMaxHeight) |
| | 2976 | | { |
| 0 | 2977 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 2978 | | videoWidth, |
| 0 | 2979 | | videoHeight, |
| 0 | 2980 | | requestedWidth, |
| 0 | 2981 | | requestedHeight, |
| 0 | 2982 | | requestedMaxWidth, |
| 0 | 2983 | | requestedMaxHeight); |
| | 2984 | |
|
| 0 | 2985 | | var isFormatFixed = !string.IsNullOrEmpty(videoFormat); |
| 0 | 2986 | | var isSizeFixed = !videoWidth.HasValue |
| 0 | 2987 | | || outWidth.Value != videoWidth.Value |
| 0 | 2988 | | || !videoHeight.HasValue |
| 0 | 2989 | | || outHeight.Value != videoHeight.Value; |
| | 2990 | |
|
| 0 | 2991 | | var swpOutW = swapOutputWandH ? outHeight.Value : outWidth.Value; |
| 0 | 2992 | | var swpOutH = swapOutputWandH ? outWidth.Value : outHeight.Value; |
| | 2993 | |
|
| 0 | 2994 | | var arg1 = isSizeFixed ? $"=w={swpOutW}:h={swpOutH}" : string.Empty; |
| 0 | 2995 | | var arg2 = isFormatFixed ? $"format={videoFormat}" : string.Empty; |
| 0 | 2996 | | if (isFormatFixed) |
| | 2997 | | { |
| 0 | 2998 | | arg2 = (isSizeFixed ? ':' : '=') + arg2; |
| | 2999 | | } |
| | 3000 | |
|
| 0 | 3001 | | if (!string.IsNullOrEmpty(hwScaleSuffix) && (isSizeFixed || isFormatFixed)) |
| | 3002 | | { |
| 0 | 3003 | | return string.Format( |
| 0 | 3004 | | CultureInfo.InvariantCulture, |
| 0 | 3005 | | "{0}_{1}{2}{3}", |
| 0 | 3006 | | hwScalePrefix ?? "scale", |
| 0 | 3007 | | hwScaleSuffix, |
| 0 | 3008 | | arg1, |
| 0 | 3009 | | arg2); |
| | 3010 | | } |
| | 3011 | |
|
| 0 | 3012 | | return string.Empty; |
| | 3013 | | } |
| | 3014 | |
|
| | 3015 | | public static string GetGraphicalSubPreProcessFilters( |
| | 3016 | | int? videoWidth, |
| | 3017 | | int? videoHeight, |
| | 3018 | | int? subtitleWidth, |
| | 3019 | | int? subtitleHeight, |
| | 3020 | | int? requestedWidth, |
| | 3021 | | int? requestedHeight, |
| | 3022 | | int? requestedMaxWidth, |
| | 3023 | | int? requestedMaxHeight) |
| | 3024 | | { |
| 0 | 3025 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 3026 | | videoWidth, |
| 0 | 3027 | | videoHeight, |
| 0 | 3028 | | requestedWidth, |
| 0 | 3029 | | requestedHeight, |
| 0 | 3030 | | requestedMaxWidth, |
| 0 | 3031 | | requestedMaxHeight); |
| | 3032 | |
|
| 0 | 3033 | | if (!outWidth.HasValue |
| 0 | 3034 | | || !outHeight.HasValue |
| 0 | 3035 | | || outWidth.Value <= 0 |
| 0 | 3036 | | || outHeight.Value <= 0) |
| | 3037 | | { |
| 0 | 3038 | | return string.Empty; |
| | 3039 | | } |
| | 3040 | |
|
| | 3041 | | // Automatically add padding based on subtitle input |
| 0 | 3042 | | var filters = @"scale,scale=-1:{1}:fast_bilinear,crop,pad=max({0}\,iw):max({1}\,ih):(ow-iw)/2:(oh-ih)/2:blac |
| | 3043 | |
|
| 0 | 3044 | | if (subtitleWidth.HasValue |
| 0 | 3045 | | && subtitleHeight.HasValue |
| 0 | 3046 | | && subtitleWidth.Value > 0 |
| 0 | 3047 | | && subtitleHeight.Value > 0) |
| | 3048 | | { |
| 0 | 3049 | | var videoDar = (double)outWidth.Value / outHeight.Value; |
| 0 | 3050 | | var subtitleDar = (double)subtitleWidth.Value / subtitleHeight.Value; |
| | 3051 | |
|
| | 3052 | | // No need to add padding when DAR is the same -> 1080p PGSSUB on 2160p video |
| 0 | 3053 | | if (Math.Abs(videoDar - subtitleDar) < 0.01f) |
| | 3054 | | { |
| 0 | 3055 | | filters = @"scale,scale={0}:{1}:fast_bilinear"; |
| | 3056 | | } |
| | 3057 | | } |
| | 3058 | |
|
| 0 | 3059 | | return string.Format( |
| 0 | 3060 | | CultureInfo.InvariantCulture, |
| 0 | 3061 | | filters, |
| 0 | 3062 | | outWidth.Value, |
| 0 | 3063 | | outHeight.Value); |
| | 3064 | | } |
| | 3065 | |
|
| | 3066 | | public static string GetAlphaSrcFilter( |
| | 3067 | | EncodingJobInfo state, |
| | 3068 | | int? videoWidth, |
| | 3069 | | int? videoHeight, |
| | 3070 | | int? requestedWidth, |
| | 3071 | | int? requestedHeight, |
| | 3072 | | int? requestedMaxWidth, |
| | 3073 | | int? requestedMaxHeight, |
| | 3074 | | float? framerate) |
| | 3075 | | { |
| 0 | 3076 | | var reqTicks = state.BaseRequest.StartTimeTicks ?? 0; |
| 0 | 3077 | | var startTime = TimeSpan.FromTicks(reqTicks).ToString(@"hh\\\:mm\\\:ss\\\.fff", CultureInfo.InvariantCulture |
| 0 | 3078 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 3079 | | videoWidth, |
| 0 | 3080 | | videoHeight, |
| 0 | 3081 | | requestedWidth, |
| 0 | 3082 | | requestedHeight, |
| 0 | 3083 | | requestedMaxWidth, |
| 0 | 3084 | | requestedMaxHeight); |
| | 3085 | |
|
| 0 | 3086 | | if (outWidth.HasValue && outHeight.HasValue) |
| | 3087 | | { |
| 0 | 3088 | | return string.Format( |
| 0 | 3089 | | CultureInfo.InvariantCulture, |
| 0 | 3090 | | "alphasrc=s={0}x{1}:r={2}:start='{3}'", |
| 0 | 3091 | | outWidth.Value, |
| 0 | 3092 | | outHeight.Value, |
| 0 | 3093 | | framerate ?? 25, |
| 0 | 3094 | | reqTicks > 0 ? startTime : 0); |
| | 3095 | | } |
| | 3096 | |
|
| 0 | 3097 | | return string.Empty; |
| | 3098 | | } |
| | 3099 | |
|
| | 3100 | | public static string GetSwScaleFilter( |
| | 3101 | | EncodingJobInfo state, |
| | 3102 | | EncodingOptions options, |
| | 3103 | | string videoEncoder, |
| | 3104 | | int? videoWidth, |
| | 3105 | | int? videoHeight, |
| | 3106 | | Video3DFormat? threedFormat, |
| | 3107 | | int? requestedWidth, |
| | 3108 | | int? requestedHeight, |
| | 3109 | | int? requestedMaxWidth, |
| | 3110 | | int? requestedMaxHeight) |
| | 3111 | | { |
| 0 | 3112 | | var isV4l2 = string.Equals(videoEncoder, "h264_v4l2m2m", StringComparison.OrdinalIgnoreCase); |
| 0 | 3113 | | var isMjpeg = videoEncoder is not null && videoEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase) |
| 0 | 3114 | | var scaleVal = isV4l2 ? 64 : 2; |
| 0 | 3115 | | var targetAr = isMjpeg ? "(a*sar)" : "a"; // manually calculate AR when using mjpeg encoder |
| | 3116 | |
|
| | 3117 | | // If fixed dimensions were supplied |
| 0 | 3118 | | if (requestedWidth.HasValue && requestedHeight.HasValue) |
| | 3119 | | { |
| 0 | 3120 | | if (isV4l2) |
| | 3121 | | { |
| 0 | 3122 | | var widthParam = requestedWidth.Value.ToString(CultureInfo.InvariantCulture); |
| 0 | 3123 | | var heightParam = requestedHeight.Value.ToString(CultureInfo.InvariantCulture); |
| | 3124 | |
|
| 0 | 3125 | | return string.Format( |
| 0 | 3126 | | CultureInfo.InvariantCulture, |
| 0 | 3127 | | "scale=trunc({0}/64)*64:trunc({1}/2)*2", |
| 0 | 3128 | | widthParam, |
| 0 | 3129 | | heightParam); |
| | 3130 | | } |
| | 3131 | |
|
| 0 | 3132 | | return GetFixedSwScaleFilter(threedFormat, requestedWidth.Value, requestedHeight.Value); |
| | 3133 | | } |
| | 3134 | |
|
| | 3135 | | // If Max dimensions were supplied, for width selects lowest even number between input width and width req s |
| | 3136 | |
|
| 0 | 3137 | | if (requestedMaxWidth.HasValue && requestedMaxHeight.HasValue) |
| | 3138 | | { |
| 0 | 3139 | | var maxWidthParam = requestedMaxWidth.Value.ToString(CultureInfo.InvariantCulture); |
| 0 | 3140 | | var maxHeightParam = requestedMaxHeight.Value.ToString(CultureInfo.InvariantCulture); |
| | 3141 | |
|
| 0 | 3142 | | return string.Format( |
| 0 | 3143 | | CultureInfo.InvariantCulture, |
| 0 | 3144 | | @"scale=trunc(min(max(iw\,ih*{3})\,min({0}\,{1}*{3}))/{2})*{2}:trunc(min(max(iw/{3}\,ih)\,min({0}/{3 |
| 0 | 3145 | | maxWidthParam, |
| 0 | 3146 | | maxHeightParam, |
| 0 | 3147 | | scaleVal, |
| 0 | 3148 | | targetAr); |
| | 3149 | | } |
| | 3150 | |
|
| | 3151 | | // If a fixed width was requested |
| 0 | 3152 | | if (requestedWidth.HasValue) |
| | 3153 | | { |
| 0 | 3154 | | if (threedFormat.HasValue) |
| | 3155 | | { |
| | 3156 | | // This method can handle 0 being passed in for the requested height |
| 0 | 3157 | | return GetFixedSwScaleFilter(threedFormat, requestedWidth.Value, 0); |
| | 3158 | | } |
| | 3159 | |
|
| 0 | 3160 | | var widthParam = requestedWidth.Value.ToString(CultureInfo.InvariantCulture); |
| | 3161 | |
|
| 0 | 3162 | | return string.Format( |
| 0 | 3163 | | CultureInfo.InvariantCulture, |
| 0 | 3164 | | "scale={0}:trunc(ow/{1}/2)*2", |
| 0 | 3165 | | widthParam, |
| 0 | 3166 | | targetAr); |
| | 3167 | | } |
| | 3168 | |
|
| | 3169 | | // If a fixed height was requested |
| 0 | 3170 | | if (requestedHeight.HasValue) |
| | 3171 | | { |
| 0 | 3172 | | var heightParam = requestedHeight.Value.ToString(CultureInfo.InvariantCulture); |
| | 3173 | |
|
| 0 | 3174 | | return string.Format( |
| 0 | 3175 | | CultureInfo.InvariantCulture, |
| 0 | 3176 | | "scale=trunc(oh*{2}/{1})*{1}:{0}", |
| 0 | 3177 | | heightParam, |
| 0 | 3178 | | scaleVal, |
| 0 | 3179 | | targetAr); |
| | 3180 | | } |
| | 3181 | |
|
| | 3182 | | // If a max width was requested |
| 0 | 3183 | | if (requestedMaxWidth.HasValue) |
| | 3184 | | { |
| 0 | 3185 | | var maxWidthParam = requestedMaxWidth.Value.ToString(CultureInfo.InvariantCulture); |
| | 3186 | |
|
| 0 | 3187 | | return string.Format( |
| 0 | 3188 | | CultureInfo.InvariantCulture, |
| 0 | 3189 | | @"scale=trunc(min(max(iw\,ih*{2})\,{0})/{1})*{1}:trunc(ow/{2}/2)*2", |
| 0 | 3190 | | maxWidthParam, |
| 0 | 3191 | | scaleVal, |
| 0 | 3192 | | targetAr); |
| | 3193 | | } |
| | 3194 | |
|
| | 3195 | | // If a max height was requested |
| 0 | 3196 | | if (requestedMaxHeight.HasValue) |
| | 3197 | | { |
| 0 | 3198 | | var maxHeightParam = requestedMaxHeight.Value.ToString(CultureInfo.InvariantCulture); |
| | 3199 | |
|
| 0 | 3200 | | return string.Format( |
| 0 | 3201 | | CultureInfo.InvariantCulture, |
| 0 | 3202 | | @"scale=trunc(oh*{2}/{1})*{1}:min(max(iw/{2}\,ih)\,{0})", |
| 0 | 3203 | | maxHeightParam, |
| 0 | 3204 | | scaleVal, |
| 0 | 3205 | | targetAr); |
| | 3206 | | } |
| | 3207 | |
|
| 0 | 3208 | | return string.Empty; |
| | 3209 | | } |
| | 3210 | |
|
| | 3211 | | private static string GetFixedSwScaleFilter(Video3DFormat? threedFormat, int requestedWidth, int requestedHeight |
| | 3212 | | { |
| 0 | 3213 | | var widthParam = requestedWidth.ToString(CultureInfo.InvariantCulture); |
| 0 | 3214 | | var heightParam = requestedHeight.ToString(CultureInfo.InvariantCulture); |
| | 3215 | |
|
| 0 | 3216 | | string filter = null; |
| | 3217 | |
|
| 0 | 3218 | | if (threedFormat.HasValue) |
| | 3219 | | { |
| 0 | 3220 | | switch (threedFormat.Value) |
| | 3221 | | { |
| | 3222 | | case Video3DFormat.HalfSideBySide: |
| 0 | 3223 | | filter = @"crop=iw/2:ih:0:0,scale=(iw*2):ih,setdar=dar=a,crop=min(iw\,ih*dar):min(ih\,iw/dar):(i |
| | 3224 | | // hsbs crop width in half,scale to correct size, set the display aspect,crop out any black bars |
| 0 | 3225 | | break; |
| | 3226 | | case Video3DFormat.FullSideBySide: |
| 0 | 3227 | | filter = @"crop=iw/2:ih:0:0,setdar=dar=a,crop=min(iw\,ih*dar):min(ih\,iw/dar):(iw-min(iw\,iw*sar |
| | 3228 | | // fsbs crop width in half,set the display aspect,crop out any black bars we may have made the s |
| 0 | 3229 | | break; |
| | 3230 | | case Video3DFormat.HalfTopAndBottom: |
| 0 | 3231 | | filter = @"crop=iw:ih/2:0:0,scale=(iw*2):ih),setdar=dar=a,crop=min(iw\,ih*dar):min(ih\,iw/dar):( |
| | 3232 | | // htab crop height in half,scale to correct size, set the display aspect,crop out any black bar |
| 0 | 3233 | | break; |
| | 3234 | | case Video3DFormat.FullTopAndBottom: |
| 0 | 3235 | | filter = @"crop=iw:ih/2:0:0,setdar=dar=a,crop=min(iw\,ih*dar):min(ih\,iw/dar):(iw-min(iw\,iw*sar |
| | 3236 | | // ftab crop height in half, set the display aspect,crop out any black bars we may have made the |
| | 3237 | | break; |
| | 3238 | | default: |
| | 3239 | | break; |
| | 3240 | | } |
| | 3241 | | } |
| | 3242 | |
|
| | 3243 | | // default |
| 0 | 3244 | | if (filter is null) |
| | 3245 | | { |
| 0 | 3246 | | if (requestedHeight > 0) |
| | 3247 | | { |
| 0 | 3248 | | filter = "scale=trunc({0}/2)*2:trunc({1}/2)*2"; |
| | 3249 | | } |
| | 3250 | | else |
| | 3251 | | { |
| 0 | 3252 | | filter = "scale={0}:trunc({0}/a/2)*2"; |
| | 3253 | | } |
| | 3254 | | } |
| | 3255 | |
|
| 0 | 3256 | | return string.Format(CultureInfo.InvariantCulture, filter, widthParam, heightParam); |
| | 3257 | | } |
| | 3258 | |
|
| | 3259 | | public static string GetSwDeinterlaceFilter(EncodingJobInfo state, EncodingOptions options) |
| | 3260 | | { |
| 0 | 3261 | | var doubleRateDeint = options.DeinterlaceDoubleRate && state.VideoStream?.ReferenceFrameRate <= 30; |
| 0 | 3262 | | return string.Format( |
| 0 | 3263 | | CultureInfo.InvariantCulture, |
| 0 | 3264 | | "{0}={1}:-1:0", |
| 0 | 3265 | | options.DeinterlaceMethod.ToString().ToLowerInvariant(), |
| 0 | 3266 | | doubleRateDeint ? "1" : "0"); |
| | 3267 | | } |
| | 3268 | |
|
| | 3269 | | public string GetHwDeinterlaceFilter(EncodingJobInfo state, EncodingOptions options, string hwDeintSuffix) |
| | 3270 | | { |
| 0 | 3271 | | var doubleRateDeint = options.DeinterlaceDoubleRate && (state.VideoStream?.ReferenceFrameRate ?? 60) <= 30; |
| 0 | 3272 | | if (hwDeintSuffix.Contains("cuda", StringComparison.OrdinalIgnoreCase)) |
| | 3273 | | { |
| 0 | 3274 | | var useBwdif = options.DeinterlaceMethod == DeinterlaceMethod.bwdif && _mediaEncoder.SupportsFilter("bwd |
| | 3275 | |
|
| 0 | 3276 | | return string.Format( |
| 0 | 3277 | | CultureInfo.InvariantCulture, |
| 0 | 3278 | | "{0}_cuda={1}:-1:0", |
| 0 | 3279 | | useBwdif ? "bwdif" : "yadif", |
| 0 | 3280 | | doubleRateDeint ? "1" : "0"); |
| | 3281 | | } |
| | 3282 | |
|
| 0 | 3283 | | if (hwDeintSuffix.Contains("vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 3284 | | { |
| 0 | 3285 | | return string.Format( |
| 0 | 3286 | | CultureInfo.InvariantCulture, |
| 0 | 3287 | | "deinterlace_vaapi=rate={0}", |
| 0 | 3288 | | doubleRateDeint ? "field" : "frame"); |
| | 3289 | | } |
| | 3290 | |
|
| 0 | 3291 | | if (hwDeintSuffix.Contains("qsv", StringComparison.OrdinalIgnoreCase)) |
| | 3292 | | { |
| 0 | 3293 | | return "deinterlace_qsv=mode=2"; |
| | 3294 | | } |
| | 3295 | |
|
| 0 | 3296 | | if (hwDeintSuffix.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase)) |
| | 3297 | | { |
| 0 | 3298 | | var useBwdif = options.DeinterlaceMethod == DeinterlaceMethod.bwdif && _mediaEncoder.SupportsFilter("bwd |
| | 3299 | |
|
| 0 | 3300 | | return string.Format( |
| 0 | 3301 | | CultureInfo.InvariantCulture, |
| 0 | 3302 | | "{0}_videotoolbox={1}:-1:0", |
| 0 | 3303 | | useBwdif ? "bwdif" : "yadif", |
| 0 | 3304 | | doubleRateDeint ? "1" : "0"); |
| | 3305 | | } |
| | 3306 | |
|
| 0 | 3307 | | return string.Empty; |
| | 3308 | | } |
| | 3309 | |
|
| | 3310 | | private string GetHwTonemapFilter(EncodingOptions options, string hwTonemapSuffix, string videoFormat, bool forc |
| | 3311 | | { |
| 0 | 3312 | | if (string.IsNullOrEmpty(hwTonemapSuffix)) |
| | 3313 | | { |
| 0 | 3314 | | return string.Empty; |
| | 3315 | | } |
| | 3316 | |
|
| 0 | 3317 | | var args = string.Empty; |
| 0 | 3318 | | var algorithm = options.TonemappingAlgorithm.ToString().ToLowerInvariant(); |
| 0 | 3319 | | var mode = options.TonemappingMode.ToString().ToLowerInvariant(); |
| 0 | 3320 | | var range = forceFullRange ? TonemappingRange.pc : options.TonemappingRange; |
| 0 | 3321 | | var rangeString = range.ToString().ToLowerInvariant(); |
| | 3322 | |
|
| 0 | 3323 | | if (string.Equals(hwTonemapSuffix, "vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 3324 | | { |
| 0 | 3325 | | var doVaVppProcamp = false; |
| 0 | 3326 | | var procampParams = string.Empty; |
| 0 | 3327 | | if (options.VppTonemappingBrightness != 0 |
| 0 | 3328 | | && options.VppTonemappingBrightness >= -100 |
| 0 | 3329 | | && options.VppTonemappingBrightness <= 100) |
| | 3330 | | { |
| 0 | 3331 | | procampParams += "procamp_vaapi=b={0}"; |
| 0 | 3332 | | doVaVppProcamp = true; |
| | 3333 | | } |
| | 3334 | |
|
| 0 | 3335 | | if (options.VppTonemappingContrast > 1 |
| 0 | 3336 | | && options.VppTonemappingContrast <= 10) |
| | 3337 | | { |
| 0 | 3338 | | procampParams += doVaVppProcamp ? ":c={1}" : "procamp_vaapi=c={1}"; |
| 0 | 3339 | | doVaVppProcamp = true; |
| | 3340 | | } |
| | 3341 | |
|
| 0 | 3342 | | args = procampParams + "{2}tonemap_vaapi=format={3}:p=bt709:t=bt709:m=bt709:extra_hw_frames=32"; |
| | 3343 | |
|
| 0 | 3344 | | return string.Format( |
| 0 | 3345 | | CultureInfo.InvariantCulture, |
| 0 | 3346 | | args, |
| 0 | 3347 | | options.VppTonemappingBrightness, |
| 0 | 3348 | | options.VppTonemappingContrast, |
| 0 | 3349 | | doVaVppProcamp ? "," : string.Empty, |
| 0 | 3350 | | videoFormat ?? "nv12"); |
| | 3351 | | } |
| | 3352 | | else |
| | 3353 | | { |
| 0 | 3354 | | args = "tonemap_{0}=format={1}:p=bt709:t=bt709:m=bt709:tonemap={2}:peak={3}:desat={4}"; |
| | 3355 | |
|
| 0 | 3356 | | var useLegacyTonemapModes = _mediaEncoder.EncoderVersion >= _minFFmpegOclCuTonemapMode |
| 0 | 3357 | | && _legacyTonemapModes.Contains(options.TonemappingMode); |
| | 3358 | |
|
| 0 | 3359 | | var useAdvancedTonemapModes = _mediaEncoder.EncoderVersion >= _minFFmpegAdvancedTonemapMode |
| 0 | 3360 | | && _advancedTonemapModes.Contains(options.TonemappingMode); |
| | 3361 | |
|
| 0 | 3362 | | if (useLegacyTonemapModes || useAdvancedTonemapModes) |
| | 3363 | | { |
| 0 | 3364 | | args += ":tonemap_mode={5}"; |
| | 3365 | | } |
| | 3366 | |
|
| 0 | 3367 | | if (options.TonemappingParam != 0) |
| | 3368 | | { |
| 0 | 3369 | | args += ":param={6}"; |
| | 3370 | | } |
| | 3371 | |
|
| 0 | 3372 | | if (range == TonemappingRange.tv || range == TonemappingRange.pc) |
| | 3373 | | { |
| 0 | 3374 | | args += ":range={7}"; |
| | 3375 | | } |
| | 3376 | | } |
| | 3377 | |
|
| 0 | 3378 | | return string.Format( |
| 0 | 3379 | | CultureInfo.InvariantCulture, |
| 0 | 3380 | | args, |
| 0 | 3381 | | hwTonemapSuffix, |
| 0 | 3382 | | videoFormat ?? "nv12", |
| 0 | 3383 | | algorithm, |
| 0 | 3384 | | options.TonemappingPeak, |
| 0 | 3385 | | options.TonemappingDesat, |
| 0 | 3386 | | mode, |
| 0 | 3387 | | options.TonemappingParam, |
| 0 | 3388 | | rangeString); |
| | 3389 | | } |
| | 3390 | |
|
| | 3391 | | private string GetLibplaceboFilter( |
| | 3392 | | EncodingOptions options, |
| | 3393 | | string videoFormat, |
| | 3394 | | bool doTonemap, |
| | 3395 | | int? videoWidth, |
| | 3396 | | int? videoHeight, |
| | 3397 | | int? requestedWidth, |
| | 3398 | | int? requestedHeight, |
| | 3399 | | int? requestedMaxWidth, |
| | 3400 | | int? requestedMaxHeight, |
| | 3401 | | bool forceFullRange) |
| | 3402 | | { |
| 0 | 3403 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 3404 | | videoWidth, |
| 0 | 3405 | | videoHeight, |
| 0 | 3406 | | requestedWidth, |
| 0 | 3407 | | requestedHeight, |
| 0 | 3408 | | requestedMaxWidth, |
| 0 | 3409 | | requestedMaxHeight); |
| | 3410 | |
|
| 0 | 3411 | | var isFormatFixed = !string.IsNullOrEmpty(videoFormat); |
| 0 | 3412 | | var isSizeFixed = !videoWidth.HasValue |
| 0 | 3413 | | || outWidth.Value != videoWidth.Value |
| 0 | 3414 | | || !videoHeight.HasValue |
| 0 | 3415 | | || outHeight.Value != videoHeight.Value; |
| | 3416 | |
|
| 0 | 3417 | | var sizeArg = isSizeFixed ? (":w=" + outWidth.Value + ":h=" + outHeight.Value) : string.Empty; |
| 0 | 3418 | | var formatArg = isFormatFixed ? (":format=" + videoFormat) : string.Empty; |
| 0 | 3419 | | var tonemapArg = string.Empty; |
| | 3420 | |
|
| 0 | 3421 | | if (doTonemap) |
| | 3422 | | { |
| 0 | 3423 | | var algorithm = options.TonemappingAlgorithm; |
| 0 | 3424 | | var algorithmString = "clip"; |
| 0 | 3425 | | var mode = options.TonemappingMode; |
| 0 | 3426 | | var range = forceFullRange ? TonemappingRange.pc : options.TonemappingRange; |
| | 3427 | |
|
| 0 | 3428 | | if (algorithm == TonemappingAlgorithm.bt2390) |
| | 3429 | | { |
| 0 | 3430 | | algorithmString = "bt.2390"; |
| | 3431 | | } |
| 0 | 3432 | | else if (algorithm != TonemappingAlgorithm.none) |
| | 3433 | | { |
| 0 | 3434 | | algorithmString = algorithm.ToString().ToLowerInvariant(); |
| | 3435 | | } |
| | 3436 | |
|
| 0 | 3437 | | tonemapArg = $":tonemapping={algorithmString}:peak_detect=0:color_primaries=bt709:color_trc=bt709:colors |
| | 3438 | |
|
| 0 | 3439 | | if (range == TonemappingRange.tv || range == TonemappingRange.pc) |
| | 3440 | | { |
| 0 | 3441 | | tonemapArg += ":range=" + range.ToString().ToLowerInvariant(); |
| | 3442 | | } |
| | 3443 | | } |
| | 3444 | |
|
| 0 | 3445 | | return string.Format( |
| 0 | 3446 | | CultureInfo.InvariantCulture, |
| 0 | 3447 | | "libplacebo=upscaler=none:downscaler=none{0}{1}{2}", |
| 0 | 3448 | | sizeArg, |
| 0 | 3449 | | formatArg, |
| 0 | 3450 | | tonemapArg); |
| | 3451 | | } |
| | 3452 | |
|
| | 3453 | | public string GetVideoTransposeDirection(EncodingJobInfo state) |
| | 3454 | | { |
| 0 | 3455 | | return (state.VideoStream?.Rotation ?? 0) switch |
| 0 | 3456 | | { |
| 0 | 3457 | | 90 => "cclock", |
| 0 | 3458 | | 180 => "reversal", |
| 0 | 3459 | | -90 => "clock", |
| 0 | 3460 | | -180 => "reversal", |
| 0 | 3461 | | _ => string.Empty |
| 0 | 3462 | | }; |
| | 3463 | | } |
| | 3464 | |
|
| | 3465 | | /// <summary> |
| | 3466 | | /// Gets the parameter of software filter chain. |
| | 3467 | | /// </summary> |
| | 3468 | | /// <param name="state">Encoding state.</param> |
| | 3469 | | /// <param name="options">Encoding options.</param> |
| | 3470 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 3471 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 3472 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetSwVidFilterChain( |
| | 3473 | | EncodingJobInfo state, |
| | 3474 | | EncodingOptions options, |
| | 3475 | | string vidEncoder) |
| | 3476 | | { |
| 0 | 3477 | | var inW = state.VideoStream?.Width; |
| 0 | 3478 | | var inH = state.VideoStream?.Height; |
| 0 | 3479 | | var reqW = state.BaseRequest.Width; |
| 0 | 3480 | | var reqH = state.BaseRequest.Height; |
| 0 | 3481 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 3482 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 3483 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 3484 | |
|
| 0 | 3485 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 3486 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 3487 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 3488 | | var isV4l2Encoder = vidEncoder.Contains("h264_v4l2m2m", StringComparison.OrdinalIgnoreCase); |
| | 3489 | |
|
| 0 | 3490 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 3491 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 3492 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 3493 | | var doToneMap = IsSwTonemapAvailable(state, options); |
| 0 | 3494 | | var requireDoviReshaping = doToneMap && state.VideoStream.VideoRangeType == VideoRangeType.DOVI; |
| | 3495 | |
|
| 0 | 3496 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 3497 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 3498 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| | 3499 | |
|
| 0 | 3500 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 3501 | | var swapWAndH = Math.Abs(rotation) == 90; |
| 0 | 3502 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 3503 | | var swpInH = swapWAndH ? inW : inH; |
| | 3504 | |
|
| | 3505 | | /* Make main filters for video stream */ |
| 0 | 3506 | | var mainFilters = new List<string>(); |
| | 3507 | |
|
| 0 | 3508 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doToneMap)); |
| | 3509 | |
|
| | 3510 | | // INPUT sw surface(memory/copy-back from vram) |
| | 3511 | | // sw deint |
| 0 | 3512 | | if (doDeintH2645) |
| | 3513 | | { |
| 0 | 3514 | | var deintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 3515 | | mainFilters.Add(deintFilter); |
| | 3516 | | } |
| | 3517 | |
|
| 0 | 3518 | | var outFormat = isSwDecoder ? "yuv420p" : "nv12"; |
| 0 | 3519 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, reqH, r |
| 0 | 3520 | | if (isVaapiEncoder) |
| | 3521 | | { |
| 0 | 3522 | | outFormat = "nv12"; |
| | 3523 | | } |
| 0 | 3524 | | else if (isV4l2Encoder) |
| | 3525 | | { |
| 0 | 3526 | | outFormat = "yuv420p"; |
| | 3527 | | } |
| | 3528 | |
|
| | 3529 | | // sw scale |
| 0 | 3530 | | mainFilters.Add(swScaleFilter); |
| | 3531 | |
|
| | 3532 | | // sw tonemap |
| 0 | 3533 | | if (doToneMap) |
| | 3534 | | { |
| | 3535 | | // tonemapx requires yuv420p10 input for dovi reshaping, let ffmpeg convert the frame when necessary |
| 0 | 3536 | | var tonemapFormat = requireDoviReshaping ? "yuv420p" : outFormat; |
| 0 | 3537 | | var tonemapArgString = "tonemapx=tonemap={0}:desat={1}:peak={2}:t=bt709:m=bt709:p=bt709:format={3}"; |
| | 3538 | |
|
| 0 | 3539 | | if (options.TonemappingParam != 0) |
| | 3540 | | { |
| 0 | 3541 | | tonemapArgString += ":param={4}"; |
| | 3542 | | } |
| | 3543 | |
|
| 0 | 3544 | | var range = options.TonemappingRange; |
| 0 | 3545 | | if (range == TonemappingRange.tv || range == TonemappingRange.pc) |
| | 3546 | | { |
| 0 | 3547 | | tonemapArgString += ":range={5}"; |
| | 3548 | | } |
| | 3549 | |
|
| 0 | 3550 | | var tonemapArgs = string.Format( |
| 0 | 3551 | | CultureInfo.InvariantCulture, |
| 0 | 3552 | | tonemapArgString, |
| 0 | 3553 | | options.TonemappingAlgorithm, |
| 0 | 3554 | | options.TonemappingDesat, |
| 0 | 3555 | | options.TonemappingPeak, |
| 0 | 3556 | | tonemapFormat, |
| 0 | 3557 | | options.TonemappingParam, |
| 0 | 3558 | | options.TonemappingRange); |
| | 3559 | |
|
| 0 | 3560 | | mainFilters.Add(tonemapArgs); |
| | 3561 | | } |
| | 3562 | | else |
| | 3563 | | { |
| | 3564 | | // OUTPUT yuv420p/nv12 surface(memory) |
| 0 | 3565 | | mainFilters.Add("format=" + outFormat); |
| | 3566 | | } |
| | 3567 | |
|
| | 3568 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 3569 | | var subFilters = new List<string>(); |
| 0 | 3570 | | var overlayFilters = new List<string>(); |
| 0 | 3571 | | if (hasTextSubs) |
| | 3572 | | { |
| | 3573 | | // subtitles=f='*.ass':alpha=0 |
| 0 | 3574 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 3575 | | mainFilters.Add(textSubtitlesFilter); |
| | 3576 | | } |
| 0 | 3577 | | else if (hasGraphicalSubs) |
| | 3578 | | { |
| 0 | 3579 | | var subW = state.SubtitleStream?.Width; |
| 0 | 3580 | | var subH = state.SubtitleStream?.Height; |
| 0 | 3581 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, reqMaxW |
| 0 | 3582 | | subFilters.Add(subPreProcFilters); |
| 0 | 3583 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 3584 | | } |
| | 3585 | |
|
| 0 | 3586 | | return (mainFilters, subFilters, overlayFilters); |
| | 3587 | | } |
| | 3588 | |
|
| | 3589 | | /// <summary> |
| | 3590 | | /// Gets the parameter of Nvidia NVENC filter chain. |
| | 3591 | | /// </summary> |
| | 3592 | | /// <param name="state">Encoding state.</param> |
| | 3593 | | /// <param name="options">Encoding options.</param> |
| | 3594 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 3595 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 3596 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetNvidiaVidFilterChain( |
| | 3597 | | EncodingJobInfo state, |
| | 3598 | | EncodingOptions options, |
| | 3599 | | string vidEncoder) |
| | 3600 | | { |
| 0 | 3601 | | if (options.HardwareAccelerationType != HardwareAccelerationType.nvenc) |
| | 3602 | | { |
| 0 | 3603 | | return (null, null, null); |
| | 3604 | | } |
| | 3605 | |
|
| 0 | 3606 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 3607 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 3608 | | var isSwEncoder = !vidEncoder.Contains("nvenc", StringComparison.OrdinalIgnoreCase); |
| | 3609 | |
|
| | 3610 | | // legacy cuvid pipeline(copy-back) |
| 0 | 3611 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 3612 | | || !IsCudaFullSupported() |
| 0 | 3613 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 3614 | | { |
| 0 | 3615 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 3616 | | } |
| | 3617 | |
|
| | 3618 | | // preferred nvdec/cuvid + cuda filters + nvenc pipeline |
| 0 | 3619 | | return GetNvidiaVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 3620 | | } |
| | 3621 | |
|
| | 3622 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetNvidiaVidFiltersPrefe |
| | 3623 | | EncodingJobInfo state, |
| | 3624 | | EncodingOptions options, |
| | 3625 | | string vidDecoder, |
| | 3626 | | string vidEncoder) |
| | 3627 | | { |
| 0 | 3628 | | var inW = state.VideoStream?.Width; |
| 0 | 3629 | | var inH = state.VideoStream?.Height; |
| 0 | 3630 | | var reqW = state.BaseRequest.Width; |
| 0 | 3631 | | var reqH = state.BaseRequest.Height; |
| 0 | 3632 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 3633 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 3634 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 3635 | |
|
| 0 | 3636 | | var isNvDecoder = vidDecoder.Contains("cuda", StringComparison.OrdinalIgnoreCase); |
| 0 | 3637 | | var isNvencEncoder = vidEncoder.Contains("nvenc", StringComparison.OrdinalIgnoreCase); |
| 0 | 3638 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 3639 | | var isSwEncoder = !isNvencEncoder; |
| 0 | 3640 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 3641 | | var isCuInCuOut = isNvDecoder && isNvencEncoder; |
| | 3642 | |
|
| 0 | 3643 | | var doubleRateDeint = options.DeinterlaceDoubleRate && (state.VideoStream?.ReferenceFrameRate ?? 60) <= 30; |
| 0 | 3644 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 3645 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 3646 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 3647 | | var doCuTonemap = IsHwTonemapAvailable(state, options); |
| | 3648 | |
|
| 0 | 3649 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 3650 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 3651 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 3652 | | var hasAssSubs = hasSubs |
| 0 | 3653 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 3654 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 3655 | | var subW = state.SubtitleStream?.Width; |
| 0 | 3656 | | var subH = state.SubtitleStream?.Height; |
| | 3657 | |
|
| 0 | 3658 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 3659 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 3660 | | var doCuTranspose = !string.IsNullOrEmpty(transposeDir) && _mediaEncoder.SupportsFilter("transpose_cuda"); |
| 0 | 3661 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isNvDecoder && doCuTranspose)); |
| 0 | 3662 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 3663 | | var swpInH = swapWAndH ? inW : inH; |
| | 3664 | |
|
| | 3665 | | /* Make main filters for video stream */ |
| 0 | 3666 | | var mainFilters = new List<string>(); |
| | 3667 | |
|
| 0 | 3668 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doCuTonemap)); |
| | 3669 | |
|
| 0 | 3670 | | if (isSwDecoder) |
| | 3671 | | { |
| | 3672 | | // INPUT sw surface(memory) |
| | 3673 | | // sw deint |
| 0 | 3674 | | if (doDeintH2645) |
| | 3675 | | { |
| 0 | 3676 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 3677 | | mainFilters.Add(swDeintFilter); |
| | 3678 | | } |
| | 3679 | |
|
| 0 | 3680 | | var outFormat = doCuTonemap ? "yuv420p10le" : "yuv420p"; |
| 0 | 3681 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| | 3682 | | // sw scale |
| 0 | 3683 | | mainFilters.Add(swScaleFilter); |
| 0 | 3684 | | mainFilters.Add($"format={outFormat}"); |
| | 3685 | |
|
| | 3686 | | // sw => hw |
| 0 | 3687 | | if (doCuTonemap) |
| | 3688 | | { |
| 0 | 3689 | | mainFilters.Add("hwupload=derive_device=cuda"); |
| | 3690 | | } |
| | 3691 | | } |
| | 3692 | |
|
| 0 | 3693 | | if (isNvDecoder) |
| | 3694 | | { |
| | 3695 | | // INPUT cuda surface(vram) |
| | 3696 | | // hw deint |
| 0 | 3697 | | if (doDeintH2645) |
| | 3698 | | { |
| 0 | 3699 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "cuda"); |
| 0 | 3700 | | mainFilters.Add(deintFilter); |
| | 3701 | | } |
| | 3702 | |
|
| | 3703 | | // hw transpose |
| 0 | 3704 | | if (doCuTranspose) |
| | 3705 | | { |
| 0 | 3706 | | mainFilters.Add($"transpose_cuda=dir={transposeDir}"); |
| | 3707 | | } |
| | 3708 | |
|
| 0 | 3709 | | var isRext = IsVideoStreamHevcRext(state); |
| 0 | 3710 | | var outFormat = doCuTonemap ? (isRext ? "p010" : string.Empty) : "yuv420p"; |
| 0 | 3711 | | var hwScaleFilter = GetHwScaleFilter("scale", "cuda", outFormat, false, swpInW, swpInH, reqW, reqH, reqM |
| | 3712 | | // hw scale |
| 0 | 3713 | | mainFilters.Add(hwScaleFilter); |
| | 3714 | | } |
| | 3715 | |
|
| | 3716 | | // hw tonemap |
| 0 | 3717 | | if (doCuTonemap) |
| | 3718 | | { |
| 0 | 3719 | | var tonemapFilter = GetHwTonemapFilter(options, "cuda", "yuv420p", isMjpegEncoder); |
| 0 | 3720 | | mainFilters.Add(tonemapFilter); |
| | 3721 | | } |
| | 3722 | |
|
| 0 | 3723 | | var memoryOutput = false; |
| 0 | 3724 | | var isUploadForCuTonemap = isSwDecoder && doCuTonemap; |
| 0 | 3725 | | if ((isNvDecoder && isSwEncoder) || (isUploadForCuTonemap && hasSubs)) |
| | 3726 | | { |
| 0 | 3727 | | memoryOutput = true; |
| | 3728 | |
|
| | 3729 | | // OUTPUT yuv420p surface(memory) |
| 0 | 3730 | | mainFilters.Add("hwdownload"); |
| 0 | 3731 | | mainFilters.Add("format=yuv420p"); |
| | 3732 | | } |
| | 3733 | |
|
| | 3734 | | // OUTPUT yuv420p surface(memory) |
| 0 | 3735 | | if (isSwDecoder && isNvencEncoder && !isUploadForCuTonemap) |
| | 3736 | | { |
| 0 | 3737 | | memoryOutput = true; |
| | 3738 | | } |
| | 3739 | |
|
| 0 | 3740 | | if (memoryOutput) |
| | 3741 | | { |
| | 3742 | | // text subtitles |
| 0 | 3743 | | if (hasTextSubs) |
| | 3744 | | { |
| 0 | 3745 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 3746 | | mainFilters.Add(textSubtitlesFilter); |
| | 3747 | | } |
| | 3748 | | } |
| | 3749 | |
|
| | 3750 | | // OUTPUT cuda(yuv420p) surface(vram) |
| | 3751 | |
|
| | 3752 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 3753 | | var subFilters = new List<string>(); |
| 0 | 3754 | | var overlayFilters = new List<string>(); |
| 0 | 3755 | | if (isCuInCuOut) |
| | 3756 | | { |
| 0 | 3757 | | if (hasSubs) |
| | 3758 | | { |
| 0 | 3759 | | if (hasGraphicalSubs) |
| | 3760 | | { |
| 0 | 3761 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 3762 | | subFilters.Add(subPreProcFilters); |
| 0 | 3763 | | subFilters.Add("format=yuva420p"); |
| | 3764 | | } |
| 0 | 3765 | | else if (hasTextSubs) |
| | 3766 | | { |
| 0 | 3767 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 3768 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 3769 | |
|
| | 3770 | | // alphasrc=s=1280x720:r=10:start=0,format=yuva420p,subtitles,hwupload |
| 0 | 3771 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subF |
| 0 | 3772 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 3773 | | subFilters.Add(alphaSrcFilter); |
| 0 | 3774 | | subFilters.Add("format=yuva420p"); |
| 0 | 3775 | | subFilters.Add(subTextSubtitlesFilter); |
| | 3776 | | } |
| | 3777 | |
|
| 0 | 3778 | | subFilters.Add("hwupload=derive_device=cuda"); |
| 0 | 3779 | | overlayFilters.Add("overlay_cuda=eof_action=pass:repeatlast=0"); |
| | 3780 | | } |
| | 3781 | | } |
| | 3782 | | else |
| | 3783 | | { |
| 0 | 3784 | | if (hasGraphicalSubs) |
| | 3785 | | { |
| 0 | 3786 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 3787 | | subFilters.Add(subPreProcFilters); |
| 0 | 3788 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 3789 | | } |
| | 3790 | | } |
| | 3791 | |
|
| 0 | 3792 | | return (mainFilters, subFilters, overlayFilters); |
| | 3793 | | } |
| | 3794 | |
|
| | 3795 | | /// <summary> |
| | 3796 | | /// Gets the parameter of AMD AMF filter chain. |
| | 3797 | | /// </summary> |
| | 3798 | | /// <param name="state">Encoding state.</param> |
| | 3799 | | /// <param name="options">Encoding options.</param> |
| | 3800 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 3801 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 3802 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAmdVidFilterChain( |
| | 3803 | | EncodingJobInfo state, |
| | 3804 | | EncodingOptions options, |
| | 3805 | | string vidEncoder) |
| | 3806 | | { |
| 0 | 3807 | | if (options.HardwareAccelerationType != HardwareAccelerationType.amf) |
| | 3808 | | { |
| 0 | 3809 | | return (null, null, null); |
| | 3810 | | } |
| | 3811 | |
|
| 0 | 3812 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 3813 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 3814 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 3815 | | var isSwEncoder = !vidEncoder.Contains("amf", StringComparison.OrdinalIgnoreCase); |
| 0 | 3816 | | var isAmfDx11OclSupported = isWindows && _mediaEncoder.SupportsHwaccel("d3d11va") && IsOpenclFullSupported() |
| | 3817 | |
|
| | 3818 | | // legacy d3d11va pipeline(copy-back) |
| 0 | 3819 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 3820 | | || !isAmfDx11OclSupported |
| 0 | 3821 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 3822 | | { |
| 0 | 3823 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 3824 | | } |
| | 3825 | |
|
| | 3826 | | // preferred d3d11va + opencl filters + amf pipeline |
| 0 | 3827 | | return GetAmdDx11VidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 3828 | | } |
| | 3829 | |
|
| | 3830 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAmdDx11VidFiltersPref |
| | 3831 | | EncodingJobInfo state, |
| | 3832 | | EncodingOptions options, |
| | 3833 | | string vidDecoder, |
| | 3834 | | string vidEncoder) |
| | 3835 | | { |
| 0 | 3836 | | var inW = state.VideoStream?.Width; |
| 0 | 3837 | | var inH = state.VideoStream?.Height; |
| 0 | 3838 | | var reqW = state.BaseRequest.Width; |
| 0 | 3839 | | var reqH = state.BaseRequest.Height; |
| 0 | 3840 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 3841 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 3842 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 3843 | |
|
| 0 | 3844 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 3845 | | var isAmfEncoder = vidEncoder.Contains("amf", StringComparison.OrdinalIgnoreCase); |
| 0 | 3846 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 3847 | | var isSwEncoder = !isAmfEncoder; |
| 0 | 3848 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 3849 | | var isDxInDxOut = isD3d11vaDecoder && isAmfEncoder; |
| | 3850 | |
|
| 0 | 3851 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 3852 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 3853 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 3854 | | var doOclTonemap = IsHwTonemapAvailable(state, options); |
| | 3855 | |
|
| 0 | 3856 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 3857 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 3858 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 3859 | | var hasAssSubs = hasSubs |
| 0 | 3860 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 3861 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 3862 | | var subW = state.SubtitleStream?.Width; |
| 0 | 3863 | | var subH = state.SubtitleStream?.Height; |
| | 3864 | |
|
| 0 | 3865 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 3866 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 3867 | | var doOclTranspose = !string.IsNullOrEmpty(transposeDir) |
| 0 | 3868 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.TransposeOpenclReversal); |
| 0 | 3869 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isD3d11vaDecoder && doOclTranspose)); |
| 0 | 3870 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 3871 | | var swpInH = swapWAndH ? inW : inH; |
| | 3872 | |
|
| | 3873 | | /* Make main filters for video stream */ |
| 0 | 3874 | | var mainFilters = new List<string>(); |
| | 3875 | |
|
| 0 | 3876 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doOclTonemap)); |
| | 3877 | |
|
| 0 | 3878 | | if (isSwDecoder) |
| | 3879 | | { |
| | 3880 | | // INPUT sw surface(memory) |
| | 3881 | | // sw deint |
| 0 | 3882 | | if (doDeintH2645) |
| | 3883 | | { |
| 0 | 3884 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 3885 | | mainFilters.Add(swDeintFilter); |
| | 3886 | | } |
| | 3887 | |
|
| 0 | 3888 | | var outFormat = doOclTonemap ? "yuv420p10le" : "yuv420p"; |
| 0 | 3889 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| | 3890 | | // sw scale |
| 0 | 3891 | | mainFilters.Add(swScaleFilter); |
| 0 | 3892 | | mainFilters.Add($"format={outFormat}"); |
| | 3893 | |
|
| | 3894 | | // keep video at memory except ocl tonemap, |
| | 3895 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 3896 | | // sw => hw |
| 0 | 3897 | | if (doOclTonemap) |
| | 3898 | | { |
| 0 | 3899 | | mainFilters.Add("hwupload=derive_device=d3d11va:extra_hw_frames=24"); |
| 0 | 3900 | | mainFilters.Add("format=d3d11"); |
| 0 | 3901 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 3902 | | } |
| | 3903 | | } |
| | 3904 | |
|
| 0 | 3905 | | if (isD3d11vaDecoder) |
| | 3906 | | { |
| | 3907 | | // INPUT d3d11 surface(vram) |
| | 3908 | | // map from d3d11va to opencl via d3d11-opencl interop. |
| 0 | 3909 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 3910 | |
|
| | 3911 | | // hw deint <= TODO: finish the 'yadif_opencl' filter |
| | 3912 | |
|
| | 3913 | | // hw transpose |
| 0 | 3914 | | if (doOclTranspose) |
| | 3915 | | { |
| 0 | 3916 | | mainFilters.Add($"transpose_opencl=dir={transposeDir}"); |
| | 3917 | | } |
| | 3918 | |
|
| 0 | 3919 | | var outFormat = doOclTonemap ? string.Empty : "nv12"; |
| 0 | 3920 | | var hwScaleFilter = GetHwScaleFilter("scale", "opencl", outFormat, false, swpInW, swpInH, reqW, reqH, re |
| | 3921 | | // hw scale |
| 0 | 3922 | | mainFilters.Add(hwScaleFilter); |
| | 3923 | | } |
| | 3924 | |
|
| | 3925 | | // hw tonemap |
| 0 | 3926 | | if (doOclTonemap) |
| | 3927 | | { |
| 0 | 3928 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 3929 | | mainFilters.Add(tonemapFilter); |
| | 3930 | | } |
| | 3931 | |
|
| 0 | 3932 | | var memoryOutput = false; |
| 0 | 3933 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 3934 | | if (isD3d11vaDecoder && isSwEncoder) |
| | 3935 | | { |
| 0 | 3936 | | memoryOutput = true; |
| | 3937 | |
|
| | 3938 | | // OUTPUT nv12 surface(memory) |
| | 3939 | | // prefer hwmap to hwdownload on opencl. |
| 0 | 3940 | | var hwTransferFilter = hasGraphicalSubs ? "hwdownload" : "hwmap=mode=read"; |
| 0 | 3941 | | mainFilters.Add(hwTransferFilter); |
| 0 | 3942 | | mainFilters.Add("format=nv12"); |
| | 3943 | | } |
| | 3944 | |
|
| | 3945 | | // OUTPUT yuv420p surface |
| 0 | 3946 | | if (isSwDecoder && isAmfEncoder && !isUploadForOclTonemap) |
| | 3947 | | { |
| 0 | 3948 | | memoryOutput = true; |
| | 3949 | | } |
| | 3950 | |
|
| 0 | 3951 | | if (memoryOutput) |
| | 3952 | | { |
| | 3953 | | // text subtitles |
| 0 | 3954 | | if (hasTextSubs) |
| | 3955 | | { |
| 0 | 3956 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 3957 | | mainFilters.Add(textSubtitlesFilter); |
| | 3958 | | } |
| | 3959 | | } |
| | 3960 | |
|
| 0 | 3961 | | if ((isDxInDxOut || isUploadForOclTonemap) && !hasSubs) |
| | 3962 | | { |
| | 3963 | | // OUTPUT d3d11(nv12) surface(vram) |
| | 3964 | | // reverse-mapping via d3d11-opencl interop. |
| 0 | 3965 | | mainFilters.Add("hwmap=derive_device=d3d11va:mode=write:reverse=1"); |
| 0 | 3966 | | mainFilters.Add("format=d3d11"); |
| | 3967 | | } |
| | 3968 | |
|
| | 3969 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 3970 | | var subFilters = new List<string>(); |
| 0 | 3971 | | var overlayFilters = new List<string>(); |
| 0 | 3972 | | if (isDxInDxOut || isUploadForOclTonemap) |
| | 3973 | | { |
| 0 | 3974 | | if (hasSubs) |
| | 3975 | | { |
| 0 | 3976 | | if (hasGraphicalSubs) |
| | 3977 | | { |
| 0 | 3978 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 3979 | | subFilters.Add(subPreProcFilters); |
| 0 | 3980 | | subFilters.Add("format=yuva420p"); |
| | 3981 | | } |
| 0 | 3982 | | else if (hasTextSubs) |
| | 3983 | | { |
| 0 | 3984 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 3985 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 3986 | |
|
| | 3987 | | // alphasrc=s=1280x720:r=10:start=0,format=yuva420p,subtitles,hwupload |
| 0 | 3988 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subF |
| 0 | 3989 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 3990 | | subFilters.Add(alphaSrcFilter); |
| 0 | 3991 | | subFilters.Add("format=yuva420p"); |
| 0 | 3992 | | subFilters.Add(subTextSubtitlesFilter); |
| | 3993 | | } |
| | 3994 | |
|
| 0 | 3995 | | subFilters.Add("hwupload=derive_device=opencl"); |
| 0 | 3996 | | overlayFilters.Add("overlay_opencl=eof_action=pass:repeatlast=0"); |
| 0 | 3997 | | overlayFilters.Add("hwmap=derive_device=d3d11va:mode=write:reverse=1"); |
| 0 | 3998 | | overlayFilters.Add("format=d3d11"); |
| | 3999 | | } |
| | 4000 | | } |
| 0 | 4001 | | else if (memoryOutput) |
| | 4002 | | { |
| 0 | 4003 | | if (hasGraphicalSubs) |
| | 4004 | | { |
| 0 | 4005 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 4006 | | subFilters.Add(subPreProcFilters); |
| 0 | 4007 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 4008 | | } |
| | 4009 | | } |
| | 4010 | |
|
| 0 | 4011 | | return (mainFilters, subFilters, overlayFilters); |
| | 4012 | | } |
| | 4013 | |
|
| | 4014 | | /// <summary> |
| | 4015 | | /// Gets the parameter of Intel QSV filter chain. |
| | 4016 | | /// </summary> |
| | 4017 | | /// <param name="state">Encoding state.</param> |
| | 4018 | | /// <param name="options">Encoding options.</param> |
| | 4019 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 4020 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 4021 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetIntelVidFilterChain( |
| | 4022 | | EncodingJobInfo state, |
| | 4023 | | EncodingOptions options, |
| | 4024 | | string vidEncoder) |
| | 4025 | | { |
| 0 | 4026 | | if (options.HardwareAccelerationType != HardwareAccelerationType.qsv) |
| | 4027 | | { |
| 0 | 4028 | | return (null, null, null); |
| | 4029 | | } |
| | 4030 | |
|
| 0 | 4031 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 4032 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 4033 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 4034 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4035 | | var isSwEncoder = !vidEncoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4036 | | var isQsvOclSupported = _mediaEncoder.SupportsHwaccel("qsv") && IsOpenclFullSupported(); |
| 0 | 4037 | | var isIntelDx11OclSupported = isWindows |
| 0 | 4038 | | && _mediaEncoder.SupportsHwaccel("d3d11va") |
| 0 | 4039 | | && isQsvOclSupported; |
| 0 | 4040 | | var isIntelVaapiOclSupported = isLinux |
| 0 | 4041 | | && IsVaapiSupported(state) |
| 0 | 4042 | | && isQsvOclSupported; |
| | 4043 | |
|
| | 4044 | | // legacy qsv pipeline(copy-back) |
| 0 | 4045 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 4046 | | || (!isIntelVaapiOclSupported && !isIntelDx11OclSupported) |
| 0 | 4047 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 4048 | | { |
| 0 | 4049 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 4050 | | } |
| | 4051 | |
|
| | 4052 | | // preferred qsv(vaapi) + opencl filters pipeline |
| 0 | 4053 | | if (isIntelVaapiOclSupported) |
| | 4054 | | { |
| 0 | 4055 | | return GetIntelQsvVaapiVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4056 | | } |
| | 4057 | |
|
| | 4058 | | // preferred qsv(d3d11) + opencl filters pipeline |
| 0 | 4059 | | if (isIntelDx11OclSupported) |
| | 4060 | | { |
| 0 | 4061 | | return GetIntelQsvDx11VidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4062 | | } |
| | 4063 | |
|
| 0 | 4064 | | return (null, null, null); |
| | 4065 | | } |
| | 4066 | |
|
| | 4067 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetIntelQsvDx11VidFilter |
| | 4068 | | EncodingJobInfo state, |
| | 4069 | | EncodingOptions options, |
| | 4070 | | string vidDecoder, |
| | 4071 | | string vidEncoder) |
| | 4072 | | { |
| 0 | 4073 | | var inW = state.VideoStream?.Width; |
| 0 | 4074 | | var inH = state.VideoStream?.Height; |
| 0 | 4075 | | var reqW = state.BaseRequest.Width; |
| 0 | 4076 | | var reqH = state.BaseRequest.Height; |
| 0 | 4077 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 4078 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 4079 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 4080 | |
|
| 0 | 4081 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 4082 | | var isQsvDecoder = vidDecoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4083 | | var isQsvEncoder = vidEncoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4084 | | var isHwDecoder = isD3d11vaDecoder || isQsvDecoder; |
| 0 | 4085 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4086 | | var isSwEncoder = !isQsvEncoder; |
| 0 | 4087 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 4088 | | var isQsvInQsvOut = isHwDecoder && isQsvEncoder; |
| | 4089 | |
|
| 0 | 4090 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 4091 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 4092 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 4093 | | var doVppTonemap = IsIntelVppTonemapAvailable(state, options); |
| 0 | 4094 | | var doOclTonemap = !doVppTonemap && IsHwTonemapAvailable(state, options); |
| 0 | 4095 | | var doTonemap = doVppTonemap || doOclTonemap; |
| | 4096 | |
|
| 0 | 4097 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 4098 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4099 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4100 | | var hasAssSubs = hasSubs |
| 0 | 4101 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 4102 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 4103 | | var subW = state.SubtitleStream?.Width; |
| 0 | 4104 | | var subH = state.SubtitleStream?.Height; |
| | 4105 | |
|
| 0 | 4106 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 4107 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 4108 | | var doVppTranspose = !string.IsNullOrEmpty(transposeDir); |
| 0 | 4109 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || ((isD3d11vaDecoder || isQsvDecoder) && doVppTran |
| 0 | 4110 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 4111 | | var swpInH = swapWAndH ? inW : inH; |
| | 4112 | |
|
| | 4113 | | /* Make main filters for video stream */ |
| 0 | 4114 | | var mainFilters = new List<string>(); |
| | 4115 | |
|
| 0 | 4116 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doTonemap)); |
| | 4117 | |
|
| 0 | 4118 | | if (isSwDecoder) |
| | 4119 | | { |
| | 4120 | | // INPUT sw surface(memory) |
| | 4121 | | // sw deint |
| 0 | 4122 | | if (doDeintH2645) |
| | 4123 | | { |
| 0 | 4124 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 4125 | | mainFilters.Add(swDeintFilter); |
| | 4126 | | } |
| | 4127 | |
|
| 0 | 4128 | | var outFormat = doOclTonemap ? "yuv420p10le" : (hasGraphicalSubs ? "yuv420p" : "nv12"); |
| 0 | 4129 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 4130 | | if (isMjpegEncoder && !doOclTonemap) |
| | 4131 | | { |
| | 4132 | | // sw decoder + hw mjpeg encoder |
| 0 | 4133 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 4134 | | } |
| | 4135 | |
|
| | 4136 | | // sw scale |
| 0 | 4137 | | mainFilters.Add(swScaleFilter); |
| 0 | 4138 | | mainFilters.Add($"format={outFormat}"); |
| | 4139 | |
|
| | 4140 | | // keep video at memory except ocl tonemap, |
| | 4141 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 4142 | | // sw => hw |
| 0 | 4143 | | if (doOclTonemap) |
| | 4144 | | { |
| 0 | 4145 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 4146 | | } |
| | 4147 | | } |
| 0 | 4148 | | else if (isD3d11vaDecoder || isQsvDecoder) |
| | 4149 | | { |
| 0 | 4150 | | var isRext = IsVideoStreamHevcRext(state); |
| 0 | 4151 | | var twoPassVppTonemap = false; |
| 0 | 4152 | | var doVppFullRangeOut = isMjpegEncoder |
| 0 | 4153 | | && _mediaEncoder.EncoderVersion >= _minFFmpegQsvVppOutRangeOption; |
| 0 | 4154 | | var doVppScaleModeHq = isMjpegEncoder |
| 0 | 4155 | | && _mediaEncoder.EncoderVersion >= _minFFmpegQsvVppScaleModeOption; |
| 0 | 4156 | | var doVppProcamp = false; |
| 0 | 4157 | | var procampParams = string.Empty; |
| 0 | 4158 | | var procampParamsString = string.Empty; |
| 0 | 4159 | | if (doVppTonemap) |
| | 4160 | | { |
| 0 | 4161 | | if (isRext) |
| | 4162 | | { |
| | 4163 | | // VPP tonemap requires p010 input |
| 0 | 4164 | | twoPassVppTonemap = true; |
| | 4165 | | } |
| | 4166 | |
|
| 0 | 4167 | | if (options.VppTonemappingBrightness != 0 |
| 0 | 4168 | | && options.VppTonemappingBrightness >= -100 |
| 0 | 4169 | | && options.VppTonemappingBrightness <= 100) |
| | 4170 | | { |
| 0 | 4171 | | procampParamsString += ":brightness={0}"; |
| 0 | 4172 | | twoPassVppTonemap = doVppProcamp = true; |
| | 4173 | | } |
| | 4174 | |
|
| 0 | 4175 | | if (options.VppTonemappingContrast > 1 |
| 0 | 4176 | | && options.VppTonemappingContrast <= 10) |
| | 4177 | | { |
| 0 | 4178 | | procampParamsString += ":contrast={1}"; |
| 0 | 4179 | | twoPassVppTonemap = doVppProcamp = true; |
| | 4180 | | } |
| | 4181 | |
|
| 0 | 4182 | | if (doVppProcamp) |
| | 4183 | | { |
| 0 | 4184 | | procampParamsString += ":procamp=1:async_depth=2"; |
| 0 | 4185 | | procampParams = string.Format( |
| 0 | 4186 | | CultureInfo.InvariantCulture, |
| 0 | 4187 | | procampParamsString, |
| 0 | 4188 | | options.VppTonemappingBrightness, |
| 0 | 4189 | | options.VppTonemappingContrast); |
| | 4190 | | } |
| | 4191 | | } |
| | 4192 | |
|
| 0 | 4193 | | var outFormat = doOclTonemap ? ((doVppTranspose || isRext) ? "p010" : string.Empty) : "nv12"; |
| 0 | 4194 | | outFormat = twoPassVppTonemap ? "p010" : outFormat; |
| | 4195 | |
|
| 0 | 4196 | | var swapOutputWandH = doVppTranspose && swapWAndH; |
| 0 | 4197 | | var hwScaleFilter = GetHwScaleFilter("vpp", "qsv", outFormat, swapOutputWandH, swpInW, swpInH, reqW, req |
| | 4198 | |
|
| 0 | 4199 | | if (!string.IsNullOrEmpty(hwScaleFilter) && doVppTranspose) |
| | 4200 | | { |
| 0 | 4201 | | hwScaleFilter += $":transpose={transposeDir}"; |
| | 4202 | | } |
| | 4203 | |
|
| 0 | 4204 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder) |
| | 4205 | | { |
| 0 | 4206 | | hwScaleFilter += (doVppFullRangeOut && !doOclTonemap) ? ":out_range=pc" : string.Empty; |
| 0 | 4207 | | hwScaleFilter += doVppScaleModeHq ? ":scale_mode=hq" : string.Empty; |
| | 4208 | | } |
| | 4209 | |
|
| 0 | 4210 | | if (!string.IsNullOrEmpty(hwScaleFilter) && doVppTonemap) |
| | 4211 | | { |
| 0 | 4212 | | hwScaleFilter += doVppProcamp ? procampParams : (twoPassVppTonemap ? string.Empty : ":tonemap=1"); |
| | 4213 | | } |
| | 4214 | |
|
| 0 | 4215 | | if (isD3d11vaDecoder) |
| | 4216 | | { |
| 0 | 4217 | | if (!string.IsNullOrEmpty(hwScaleFilter) || doDeintH2645) |
| | 4218 | | { |
| | 4219 | | // INPUT d3d11 surface(vram) |
| | 4220 | | // map from d3d11va to qsv. |
| 0 | 4221 | | mainFilters.Add("hwmap=derive_device=qsv"); |
| | 4222 | | } |
| | 4223 | | } |
| | 4224 | |
|
| | 4225 | | // hw deint |
| 0 | 4226 | | if (doDeintH2645) |
| | 4227 | | { |
| 0 | 4228 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "qsv"); |
| 0 | 4229 | | mainFilters.Add(deintFilter); |
| | 4230 | | } |
| | 4231 | |
|
| | 4232 | | // hw transpose & scale & tonemap(w/o procamp) |
| 0 | 4233 | | mainFilters.Add(hwScaleFilter); |
| | 4234 | |
|
| | 4235 | | // hw tonemap(w/ procamp) |
| 0 | 4236 | | if (doVppTonemap && twoPassVppTonemap) |
| | 4237 | | { |
| 0 | 4238 | | mainFilters.Add("vpp_qsv=tonemap=1:format=nv12:async_depth=2"); |
| | 4239 | | } |
| | 4240 | |
|
| | 4241 | | // force bt709 just in case vpp tonemap is not triggered or using MSDK instead of VPL. |
| 0 | 4242 | | if (doVppTonemap) |
| | 4243 | | { |
| 0 | 4244 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, false)); |
| | 4245 | | } |
| | 4246 | | } |
| | 4247 | |
|
| 0 | 4248 | | if (doOclTonemap && isHwDecoder) |
| | 4249 | | { |
| | 4250 | | // map from qsv to opencl via qsv(d3d11)-opencl interop. |
| 0 | 4251 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 4252 | | } |
| | 4253 | |
|
| | 4254 | | // hw tonemap |
| 0 | 4255 | | if (doOclTonemap) |
| | 4256 | | { |
| 0 | 4257 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 4258 | | mainFilters.Add(tonemapFilter); |
| | 4259 | | } |
| | 4260 | |
|
| 0 | 4261 | | var memoryOutput = false; |
| 0 | 4262 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 4263 | | var isHwmapUsable = isSwEncoder && doOclTonemap; |
| 0 | 4264 | | if ((isHwDecoder && isSwEncoder) || isUploadForOclTonemap) |
| | 4265 | | { |
| 0 | 4266 | | memoryOutput = true; |
| | 4267 | |
|
| | 4268 | | // OUTPUT nv12 surface(memory) |
| | 4269 | | // prefer hwmap to hwdownload on opencl. |
| | 4270 | | // qsv hwmap is not fully implemented for the time being. |
| 0 | 4271 | | mainFilters.Add(isHwmapUsable ? "hwmap=mode=read" : "hwdownload"); |
| 0 | 4272 | | mainFilters.Add("format=nv12"); |
| | 4273 | | } |
| | 4274 | |
|
| | 4275 | | // OUTPUT nv12 surface(memory) |
| 0 | 4276 | | if (isSwDecoder && isQsvEncoder) |
| | 4277 | | { |
| 0 | 4278 | | memoryOutput = true; |
| | 4279 | | } |
| | 4280 | |
|
| 0 | 4281 | | if (memoryOutput) |
| | 4282 | | { |
| | 4283 | | // text subtitles |
| 0 | 4284 | | if (hasTextSubs) |
| | 4285 | | { |
| 0 | 4286 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 4287 | | mainFilters.Add(textSubtitlesFilter); |
| | 4288 | | } |
| | 4289 | | } |
| | 4290 | |
|
| 0 | 4291 | | if (isQsvInQsvOut && doOclTonemap) |
| | 4292 | | { |
| | 4293 | | // OUTPUT qsv(nv12) surface(vram) |
| | 4294 | | // reverse-mapping via qsv(d3d11)-opencl interop. |
| 0 | 4295 | | mainFilters.Add("hwmap=derive_device=qsv:mode=write:reverse=1"); |
| 0 | 4296 | | mainFilters.Add("format=qsv"); |
| | 4297 | | } |
| | 4298 | |
|
| | 4299 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 4300 | | var subFilters = new List<string>(); |
| 0 | 4301 | | var overlayFilters = new List<string>(); |
| 0 | 4302 | | if (isQsvInQsvOut) |
| | 4303 | | { |
| 0 | 4304 | | if (hasSubs) |
| | 4305 | | { |
| 0 | 4306 | | if (hasGraphicalSubs) |
| | 4307 | | { |
| | 4308 | | // overlay_qsv can handle overlay scaling, setup a smaller height to reduce transfer overhead |
| 0 | 4309 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 4310 | | subFilters.Add(subPreProcFilters); |
| 0 | 4311 | | subFilters.Add("format=bgra"); |
| | 4312 | | } |
| 0 | 4313 | | else if (hasTextSubs) |
| | 4314 | | { |
| 0 | 4315 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 4316 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 4317 | |
|
| | 4318 | | // alphasrc=s=1280x720:r=10:start=0,format=bgra,subtitles,hwupload |
| 0 | 4319 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, 1080, subFram |
| 0 | 4320 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 4321 | | subFilters.Add(alphaSrcFilter); |
| 0 | 4322 | | subFilters.Add("format=bgra"); |
| 0 | 4323 | | subFilters.Add(subTextSubtitlesFilter); |
| | 4324 | | } |
| | 4325 | |
|
| | 4326 | | // qsv requires a fixed pool size. |
| | 4327 | | // default to 64 otherwise it will fail on certain iGPU. |
| 0 | 4328 | | subFilters.Add("hwupload=derive_device=qsv:extra_hw_frames=64"); |
| | 4329 | |
|
| 0 | 4330 | | var (overlayW, overlayH) = GetFixedOutputSize(swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH); |
| 0 | 4331 | | var overlaySize = (overlayW.HasValue && overlayH.HasValue) |
| 0 | 4332 | | ? $":w={overlayW.Value}:h={overlayH.Value}" |
| 0 | 4333 | | : string.Empty; |
| 0 | 4334 | | var overlayQsvFilter = string.Format( |
| 0 | 4335 | | CultureInfo.InvariantCulture, |
| 0 | 4336 | | "overlay_qsv=eof_action=pass:repeatlast=0{0}", |
| 0 | 4337 | | overlaySize); |
| 0 | 4338 | | overlayFilters.Add(overlayQsvFilter); |
| | 4339 | | } |
| | 4340 | | } |
| 0 | 4341 | | else if (memoryOutput) |
| | 4342 | | { |
| 0 | 4343 | | if (hasGraphicalSubs) |
| | 4344 | | { |
| 0 | 4345 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 4346 | | subFilters.Add(subPreProcFilters); |
| 0 | 4347 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 4348 | | } |
| | 4349 | | } |
| | 4350 | |
|
| 0 | 4351 | | return (mainFilters, subFilters, overlayFilters); |
| | 4352 | | } |
| | 4353 | |
|
| | 4354 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetIntelQsvVaapiVidFilte |
| | 4355 | | EncodingJobInfo state, |
| | 4356 | | EncodingOptions options, |
| | 4357 | | string vidDecoder, |
| | 4358 | | string vidEncoder) |
| | 4359 | | { |
| 0 | 4360 | | var inW = state.VideoStream?.Width; |
| 0 | 4361 | | var inH = state.VideoStream?.Height; |
| 0 | 4362 | | var reqW = state.BaseRequest.Width; |
| 0 | 4363 | | var reqH = state.BaseRequest.Height; |
| 0 | 4364 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 4365 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 4366 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 4367 | |
|
| 0 | 4368 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4369 | | var isQsvDecoder = vidDecoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4370 | | var isQsvEncoder = vidEncoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4371 | | var isHwDecoder = isVaapiDecoder || isQsvDecoder; |
| 0 | 4372 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4373 | | var isSwEncoder = !isQsvEncoder; |
| 0 | 4374 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 4375 | | var isQsvInQsvOut = isHwDecoder && isQsvEncoder; |
| | 4376 | |
|
| 0 | 4377 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 4378 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 4379 | | var doVaVppTonemap = IsIntelVppTonemapAvailable(state, options); |
| 0 | 4380 | | var doOclTonemap = !doVaVppTonemap && IsHwTonemapAvailable(state, options); |
| 0 | 4381 | | var doTonemap = doVaVppTonemap || doOclTonemap; |
| 0 | 4382 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| | 4383 | |
|
| 0 | 4384 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 4385 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4386 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4387 | | var hasAssSubs = hasSubs |
| 0 | 4388 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 4389 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 4390 | | var subW = state.SubtitleStream?.Width; |
| 0 | 4391 | | var subH = state.SubtitleStream?.Height; |
| | 4392 | |
|
| 0 | 4393 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 4394 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 4395 | | var doVppTranspose = !string.IsNullOrEmpty(transposeDir); |
| 0 | 4396 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || ((isVaapiDecoder || isQsvDecoder) && doVppTransp |
| 0 | 4397 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 4398 | | var swpInH = swapWAndH ? inW : inH; |
| | 4399 | |
|
| | 4400 | | /* Make main filters for video stream */ |
| 0 | 4401 | | var mainFilters = new List<string>(); |
| | 4402 | |
|
| 0 | 4403 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doTonemap)); |
| | 4404 | |
|
| 0 | 4405 | | if (isSwDecoder) |
| | 4406 | | { |
| | 4407 | | // INPUT sw surface(memory) |
| | 4408 | | // sw deint |
| 0 | 4409 | | if (doDeintH2645) |
| | 4410 | | { |
| 0 | 4411 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 4412 | | mainFilters.Add(swDeintFilter); |
| | 4413 | | } |
| | 4414 | |
|
| 0 | 4415 | | var outFormat = doOclTonemap ? "yuv420p10le" : (hasGraphicalSubs ? "yuv420p" : "nv12"); |
| 0 | 4416 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 4417 | | if (isMjpegEncoder && !doOclTonemap) |
| | 4418 | | { |
| | 4419 | | // sw decoder + hw mjpeg encoder |
| 0 | 4420 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 4421 | | } |
| | 4422 | |
|
| | 4423 | | // sw scale |
| 0 | 4424 | | mainFilters.Add(swScaleFilter); |
| 0 | 4425 | | mainFilters.Add($"format={outFormat}"); |
| | 4426 | |
|
| | 4427 | | // keep video at memory except ocl tonemap, |
| | 4428 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 4429 | | // sw => hw |
| 0 | 4430 | | if (doOclTonemap) |
| | 4431 | | { |
| 0 | 4432 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 4433 | | } |
| | 4434 | | } |
| 0 | 4435 | | else if (isVaapiDecoder || isQsvDecoder) |
| | 4436 | | { |
| 0 | 4437 | | var hwFilterSuffix = isVaapiDecoder ? "vaapi" : "qsv"; |
| 0 | 4438 | | var isRext = IsVideoStreamHevcRext(state); |
| 0 | 4439 | | var doVppFullRangeOut = isMjpegEncoder |
| 0 | 4440 | | && _mediaEncoder.EncoderVersion >= _minFFmpegQsvVppOutRangeOption; |
| 0 | 4441 | | var doVppScaleModeHq = isMjpegEncoder |
| 0 | 4442 | | && _mediaEncoder.EncoderVersion >= _minFFmpegQsvVppScaleModeOption; |
| | 4443 | |
|
| | 4444 | | // INPUT vaapi/qsv surface(vram) |
| | 4445 | | // hw deint |
| 0 | 4446 | | if (doDeintH2645) |
| | 4447 | | { |
| 0 | 4448 | | var deintFilter = GetHwDeinterlaceFilter(state, options, hwFilterSuffix); |
| 0 | 4449 | | mainFilters.Add(deintFilter); |
| | 4450 | | } |
| | 4451 | |
|
| | 4452 | | // hw transpose(vaapi vpp) |
| 0 | 4453 | | if (isVaapiDecoder && doVppTranspose) |
| | 4454 | | { |
| 0 | 4455 | | mainFilters.Add($"transpose_vaapi=dir={transposeDir}"); |
| | 4456 | | } |
| | 4457 | |
|
| 0 | 4458 | | var outFormat = doTonemap ? (((isQsvDecoder && doVppTranspose) || isRext) ? "p010" : string.Empty) : "nv |
| 0 | 4459 | | var swapOutputWandH = isQsvDecoder && doVppTranspose && swapWAndH; |
| 0 | 4460 | | var hwScalePrefix = isQsvDecoder ? "vpp" : "scale"; |
| 0 | 4461 | | var hwScaleFilter = GetHwScaleFilter(hwScalePrefix, hwFilterSuffix, outFormat, swapOutputWandH, swpInW, |
| | 4462 | |
|
| 0 | 4463 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isQsvDecoder && doVppTranspose) |
| | 4464 | | { |
| 0 | 4465 | | hwScaleFilter += $":transpose={transposeDir}"; |
| | 4466 | | } |
| | 4467 | |
|
| 0 | 4468 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder) |
| | 4469 | | { |
| 0 | 4470 | | hwScaleFilter += ((isQsvDecoder && !doVppFullRangeOut) || doOclTonemap) ? string.Empty : ":out_range |
| 0 | 4471 | | hwScaleFilter += isQsvDecoder ? (doVppScaleModeHq ? ":scale_mode=hq" : string.Empty) : ":mode=hq"; |
| | 4472 | | } |
| | 4473 | |
|
| | 4474 | | // allocate extra pool sizes for vaapi vpp scale |
| 0 | 4475 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isVaapiDecoder) |
| | 4476 | | { |
| 0 | 4477 | | hwScaleFilter += ":extra_hw_frames=24"; |
| | 4478 | | } |
| | 4479 | |
|
| | 4480 | | // hw transpose(qsv vpp) & scale |
| 0 | 4481 | | mainFilters.Add(hwScaleFilter); |
| | 4482 | | } |
| | 4483 | |
|
| | 4484 | | // vaapi vpp tonemap |
| 0 | 4485 | | if (doVaVppTonemap && isHwDecoder) |
| | 4486 | | { |
| 0 | 4487 | | if (isQsvDecoder) |
| | 4488 | | { |
| | 4489 | | // map from qsv to vaapi. |
| 0 | 4490 | | mainFilters.Add("hwmap=derive_device=vaapi"); |
| 0 | 4491 | | mainFilters.Add("format=vaapi"); |
| | 4492 | | } |
| | 4493 | |
|
| 0 | 4494 | | var tonemapFilter = GetHwTonemapFilter(options, "vaapi", "nv12", isMjpegEncoder); |
| 0 | 4495 | | mainFilters.Add(tonemapFilter); |
| | 4496 | |
|
| 0 | 4497 | | if (isQsvDecoder) |
| | 4498 | | { |
| | 4499 | | // map from vaapi to qsv. |
| 0 | 4500 | | mainFilters.Add("hwmap=derive_device=qsv"); |
| 0 | 4501 | | mainFilters.Add("format=qsv"); |
| | 4502 | | } |
| | 4503 | | } |
| | 4504 | |
|
| 0 | 4505 | | if (doOclTonemap && isHwDecoder) |
| | 4506 | | { |
| | 4507 | | // map from qsv to opencl via qsv(vaapi)-opencl interop. |
| 0 | 4508 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 4509 | | } |
| | 4510 | |
|
| | 4511 | | // ocl tonemap |
| 0 | 4512 | | if (doOclTonemap) |
| | 4513 | | { |
| 0 | 4514 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 4515 | | mainFilters.Add(tonemapFilter); |
| | 4516 | | } |
| | 4517 | |
|
| 0 | 4518 | | var memoryOutput = false; |
| 0 | 4519 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 4520 | | var isHwmapUsable = isSwEncoder && (doOclTonemap || isVaapiDecoder); |
| 0 | 4521 | | if ((isHwDecoder && isSwEncoder) || isUploadForOclTonemap) |
| | 4522 | | { |
| 0 | 4523 | | memoryOutput = true; |
| | 4524 | |
|
| | 4525 | | // OUTPUT nv12 surface(memory) |
| | 4526 | | // prefer hwmap to hwdownload on opencl/vaapi. |
| | 4527 | | // qsv hwmap is not fully implemented for the time being. |
| 0 | 4528 | | mainFilters.Add(isHwmapUsable ? "hwmap=mode=read" : "hwdownload"); |
| 0 | 4529 | | mainFilters.Add("format=nv12"); |
| | 4530 | | } |
| | 4531 | |
|
| | 4532 | | // OUTPUT nv12 surface(memory) |
| 0 | 4533 | | if (isSwDecoder && isQsvEncoder) |
| | 4534 | | { |
| 0 | 4535 | | memoryOutput = true; |
| | 4536 | | } |
| | 4537 | |
|
| 0 | 4538 | | if (memoryOutput) |
| | 4539 | | { |
| | 4540 | | // text subtitles |
| 0 | 4541 | | if (hasTextSubs) |
| | 4542 | | { |
| 0 | 4543 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 4544 | | mainFilters.Add(textSubtitlesFilter); |
| | 4545 | | } |
| | 4546 | | } |
| | 4547 | |
|
| 0 | 4548 | | if (isQsvInQsvOut) |
| | 4549 | | { |
| 0 | 4550 | | if (doOclTonemap) |
| | 4551 | | { |
| | 4552 | | // OUTPUT qsv(nv12) surface(vram) |
| | 4553 | | // reverse-mapping via qsv(vaapi)-opencl interop. |
| | 4554 | | // add extra pool size to avoid the 'cannot allocate memory' error on hevc_qsv. |
| 0 | 4555 | | mainFilters.Add("hwmap=derive_device=qsv:mode=write:reverse=1:extra_hw_frames=16"); |
| 0 | 4556 | | mainFilters.Add("format=qsv"); |
| | 4557 | | } |
| 0 | 4558 | | else if (isVaapiDecoder) |
| | 4559 | | { |
| 0 | 4560 | | mainFilters.Add("hwmap=derive_device=qsv"); |
| 0 | 4561 | | mainFilters.Add("format=qsv"); |
| | 4562 | | } |
| | 4563 | | } |
| | 4564 | |
|
| | 4565 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 4566 | | var subFilters = new List<string>(); |
| 0 | 4567 | | var overlayFilters = new List<string>(); |
| 0 | 4568 | | if (isQsvInQsvOut) |
| | 4569 | | { |
| 0 | 4570 | | if (hasSubs) |
| | 4571 | | { |
| 0 | 4572 | | if (hasGraphicalSubs) |
| | 4573 | | { |
| | 4574 | | // overlay_qsv can handle overlay scaling, setup a smaller height to reduce transfer overhead |
| 0 | 4575 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 4576 | | subFilters.Add(subPreProcFilters); |
| 0 | 4577 | | subFilters.Add("format=bgra"); |
| | 4578 | | } |
| 0 | 4579 | | else if (hasTextSubs) |
| | 4580 | | { |
| 0 | 4581 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 4582 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 4583 | |
|
| 0 | 4584 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, 1080, subFram |
| 0 | 4585 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 4586 | | subFilters.Add(alphaSrcFilter); |
| 0 | 4587 | | subFilters.Add("format=bgra"); |
| 0 | 4588 | | subFilters.Add(subTextSubtitlesFilter); |
| | 4589 | | } |
| | 4590 | |
|
| | 4591 | | // qsv requires a fixed pool size. |
| | 4592 | | // default to 64 otherwise it will fail on certain iGPU. |
| 0 | 4593 | | subFilters.Add("hwupload=derive_device=qsv:extra_hw_frames=64"); |
| | 4594 | |
|
| 0 | 4595 | | var (overlayW, overlayH) = GetFixedOutputSize(swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH); |
| 0 | 4596 | | var overlaySize = (overlayW.HasValue && overlayH.HasValue) |
| 0 | 4597 | | ? $":w={overlayW.Value}:h={overlayH.Value}" |
| 0 | 4598 | | : string.Empty; |
| 0 | 4599 | | var overlayQsvFilter = string.Format( |
| 0 | 4600 | | CultureInfo.InvariantCulture, |
| 0 | 4601 | | "overlay_qsv=eof_action=pass:repeatlast=0{0}", |
| 0 | 4602 | | overlaySize); |
| 0 | 4603 | | overlayFilters.Add(overlayQsvFilter); |
| | 4604 | | } |
| | 4605 | | } |
| 0 | 4606 | | else if (memoryOutput) |
| | 4607 | | { |
| 0 | 4608 | | if (hasGraphicalSubs) |
| | 4609 | | { |
| 0 | 4610 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 4611 | | subFilters.Add(subPreProcFilters); |
| 0 | 4612 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 4613 | | } |
| | 4614 | | } |
| | 4615 | |
|
| 0 | 4616 | | return (mainFilters, subFilters, overlayFilters); |
| | 4617 | | } |
| | 4618 | |
|
| | 4619 | | /// <summary> |
| | 4620 | | /// Gets the parameter of Intel/AMD VAAPI filter chain. |
| | 4621 | | /// </summary> |
| | 4622 | | /// <param name="state">Encoding state.</param> |
| | 4623 | | /// <param name="options">Encoding options.</param> |
| | 4624 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 4625 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 4626 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetVaapiVidFilterChain( |
| | 4627 | | EncodingJobInfo state, |
| | 4628 | | EncodingOptions options, |
| | 4629 | | string vidEncoder) |
| | 4630 | | { |
| 0 | 4631 | | if (options.HardwareAccelerationType != HardwareAccelerationType.vaapi) |
| | 4632 | | { |
| 0 | 4633 | | return (null, null, null); |
| | 4634 | | } |
| | 4635 | |
|
| 0 | 4636 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 4637 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 4638 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4639 | | var isSwEncoder = !vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4640 | | var isVaapiFullSupported = isLinux && IsVaapiSupported(state) && IsVaapiFullSupported(); |
| 0 | 4641 | | var isVaapiOclSupported = isVaapiFullSupported && IsOpenclFullSupported(); |
| 0 | 4642 | | var isVaapiVkSupported = isVaapiFullSupported && IsVulkanFullSupported(); |
| | 4643 | |
|
| | 4644 | | // legacy vaapi pipeline(copy-back) |
| 0 | 4645 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 4646 | | || !isVaapiOclSupported |
| 0 | 4647 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 4648 | | { |
| 0 | 4649 | | var swFilterChain = GetSwVidFilterChain(state, options, vidEncoder); |
| | 4650 | |
|
| 0 | 4651 | | if (!isSwEncoder) |
| | 4652 | | { |
| 0 | 4653 | | var newfilters = new List<string>(); |
| 0 | 4654 | | var noOverlay = swFilterChain.OverlayFilters.Count == 0; |
| 0 | 4655 | | newfilters.AddRange(noOverlay ? swFilterChain.MainFilters : swFilterChain.OverlayFilters); |
| 0 | 4656 | | newfilters.Add("hwupload=derive_device=vaapi"); |
| | 4657 | |
|
| 0 | 4658 | | var mainFilters = noOverlay ? newfilters : swFilterChain.MainFilters; |
| 0 | 4659 | | var overlayFilters = noOverlay ? swFilterChain.OverlayFilters : newfilters; |
| 0 | 4660 | | return (mainFilters, swFilterChain.SubFilters, overlayFilters); |
| | 4661 | | } |
| | 4662 | |
|
| 0 | 4663 | | return swFilterChain; |
| | 4664 | | } |
| | 4665 | |
|
| | 4666 | | // preferred vaapi + opencl filters pipeline |
| 0 | 4667 | | if (_mediaEncoder.IsVaapiDeviceInteliHD) |
| | 4668 | | { |
| | 4669 | | // Intel iHD path, with extra vpp tonemap and overlay support. |
| 0 | 4670 | | return GetIntelVaapiFullVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4671 | | } |
| | 4672 | |
|
| | 4673 | | // preferred vaapi + vulkan filters pipeline |
| 0 | 4674 | | if (_mediaEncoder.IsVaapiDeviceAmd |
| 0 | 4675 | | && isVaapiVkSupported |
| 0 | 4676 | | && _mediaEncoder.IsVaapiDeviceSupportVulkanDrmInterop |
| 0 | 4677 | | && Environment.OSVersion.Version >= _minKernelVersionAmdVkFmtModifier) |
| | 4678 | | { |
| | 4679 | | // AMD radeonsi path(targeting Polaris/gfx8+), with extra vulkan tonemap and overlay support. |
| 0 | 4680 | | return GetAmdVaapiFullVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4681 | | } |
| | 4682 | |
|
| | 4683 | | // Intel i965 and Amd legacy driver path, only featuring scale and deinterlace support. |
| 0 | 4684 | | return GetVaapiLimitedVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4685 | | } |
| | 4686 | |
|
| | 4687 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetIntelVaapiFullVidFilt |
| | 4688 | | EncodingJobInfo state, |
| | 4689 | | EncodingOptions options, |
| | 4690 | | string vidDecoder, |
| | 4691 | | string vidEncoder) |
| | 4692 | | { |
| 0 | 4693 | | var inW = state.VideoStream?.Width; |
| 0 | 4694 | | var inH = state.VideoStream?.Height; |
| 0 | 4695 | | var reqW = state.BaseRequest.Width; |
| 0 | 4696 | | var reqH = state.BaseRequest.Height; |
| 0 | 4697 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 4698 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 4699 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 4700 | |
|
| 0 | 4701 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4702 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4703 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4704 | | var isSwEncoder = !isVaapiEncoder; |
| 0 | 4705 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 4706 | | var isVaInVaOut = isVaapiDecoder && isVaapiEncoder; |
| | 4707 | |
|
| 0 | 4708 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 4709 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 4710 | | var doVaVppTonemap = isVaapiDecoder && IsIntelVppTonemapAvailable(state, options); |
| 0 | 4711 | | var doOclTonemap = !doVaVppTonemap && IsHwTonemapAvailable(state, options); |
| 0 | 4712 | | var doTonemap = doVaVppTonemap || doOclTonemap; |
| 0 | 4713 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| | 4714 | |
|
| 0 | 4715 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 4716 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4717 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4718 | | var hasAssSubs = hasSubs |
| 0 | 4719 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 4720 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 4721 | | var subW = state.SubtitleStream?.Width; |
| 0 | 4722 | | var subH = state.SubtitleStream?.Height; |
| | 4723 | |
|
| 0 | 4724 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 4725 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 4726 | | var doVaVppTranspose = !string.IsNullOrEmpty(transposeDir); |
| 0 | 4727 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isVaapiDecoder && doVaVppTranspose)); |
| 0 | 4728 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 4729 | | var swpInH = swapWAndH ? inW : inH; |
| | 4730 | |
|
| | 4731 | | /* Make main filters for video stream */ |
| 0 | 4732 | | var mainFilters = new List<string>(); |
| | 4733 | |
|
| 0 | 4734 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doTonemap)); |
| | 4735 | |
|
| 0 | 4736 | | if (isSwDecoder) |
| | 4737 | | { |
| | 4738 | | // INPUT sw surface(memory) |
| | 4739 | | // sw deint |
| 0 | 4740 | | if (doDeintH2645) |
| | 4741 | | { |
| 0 | 4742 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 4743 | | mainFilters.Add(swDeintFilter); |
| | 4744 | | } |
| | 4745 | |
|
| 0 | 4746 | | var outFormat = doOclTonemap ? "yuv420p10le" : "nv12"; |
| 0 | 4747 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 4748 | | if (isMjpegEncoder && !doOclTonemap) |
| | 4749 | | { |
| | 4750 | | // sw decoder + hw mjpeg encoder |
| 0 | 4751 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 4752 | | } |
| | 4753 | |
|
| | 4754 | | // sw scale |
| 0 | 4755 | | mainFilters.Add(swScaleFilter); |
| 0 | 4756 | | mainFilters.Add($"format={outFormat}"); |
| | 4757 | |
|
| | 4758 | | // keep video at memory except ocl tonemap, |
| | 4759 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 4760 | | // sw => hw |
| 0 | 4761 | | if (doOclTonemap) |
| | 4762 | | { |
| 0 | 4763 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 4764 | | } |
| | 4765 | | } |
| 0 | 4766 | | else if (isVaapiDecoder) |
| | 4767 | | { |
| 0 | 4768 | | var isRext = IsVideoStreamHevcRext(state); |
| | 4769 | |
|
| | 4770 | | // INPUT vaapi surface(vram) |
| | 4771 | | // hw deint |
| 0 | 4772 | | if (doDeintH2645) |
| | 4773 | | { |
| 0 | 4774 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 4775 | | mainFilters.Add(deintFilter); |
| | 4776 | | } |
| | 4777 | |
|
| | 4778 | | // hw transpose |
| 0 | 4779 | | if (doVaVppTranspose) |
| | 4780 | | { |
| 0 | 4781 | | mainFilters.Add($"transpose_vaapi=dir={transposeDir}"); |
| | 4782 | | } |
| | 4783 | |
|
| 0 | 4784 | | var outFormat = doTonemap ? (isRext ? "p010" : string.Empty) : "nv12"; |
| 0 | 4785 | | var hwScaleFilter = GetHwScaleFilter("scale", "vaapi", outFormat, false, swpInW, swpInH, reqW, reqH, req |
| | 4786 | |
|
| 0 | 4787 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder) |
| | 4788 | | { |
| 0 | 4789 | | hwScaleFilter += doOclTonemap ? string.Empty : ":out_range=pc"; |
| 0 | 4790 | | hwScaleFilter += ":mode=hq"; |
| | 4791 | | } |
| | 4792 | |
|
| | 4793 | | // allocate extra pool sizes for vaapi vpp |
| 0 | 4794 | | if (!string.IsNullOrEmpty(hwScaleFilter)) |
| | 4795 | | { |
| 0 | 4796 | | hwScaleFilter += ":extra_hw_frames=24"; |
| | 4797 | | } |
| | 4798 | |
|
| | 4799 | | // hw scale |
| 0 | 4800 | | mainFilters.Add(hwScaleFilter); |
| | 4801 | | } |
| | 4802 | |
|
| | 4803 | | // vaapi vpp tonemap |
| 0 | 4804 | | if (doVaVppTonemap && isVaapiDecoder) |
| | 4805 | | { |
| 0 | 4806 | | var tonemapFilter = GetHwTonemapFilter(options, "vaapi", "nv12", isMjpegEncoder); |
| 0 | 4807 | | mainFilters.Add(tonemapFilter); |
| | 4808 | | } |
| | 4809 | |
|
| 0 | 4810 | | if (doOclTonemap && isVaapiDecoder) |
| | 4811 | | { |
| | 4812 | | // map from vaapi to opencl via vaapi-opencl interop(Intel only). |
| 0 | 4813 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 4814 | | } |
| | 4815 | |
|
| | 4816 | | // ocl tonemap |
| 0 | 4817 | | if (doOclTonemap) |
| | 4818 | | { |
| 0 | 4819 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 4820 | | mainFilters.Add(tonemapFilter); |
| | 4821 | | } |
| | 4822 | |
|
| 0 | 4823 | | if (doOclTonemap && isVaInVaOut) |
| | 4824 | | { |
| | 4825 | | // OUTPUT vaapi(nv12) surface(vram) |
| | 4826 | | // reverse-mapping via vaapi-opencl interop. |
| 0 | 4827 | | mainFilters.Add("hwmap=derive_device=vaapi:mode=write:reverse=1"); |
| 0 | 4828 | | mainFilters.Add("format=vaapi"); |
| | 4829 | | } |
| | 4830 | |
|
| 0 | 4831 | | var memoryOutput = false; |
| 0 | 4832 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 4833 | | var isHwmapNotUsable = isUploadForOclTonemap && isVaapiEncoder; |
| 0 | 4834 | | if ((isVaapiDecoder && isSwEncoder) || isUploadForOclTonemap) |
| | 4835 | | { |
| 0 | 4836 | | memoryOutput = true; |
| | 4837 | |
|
| | 4838 | | // OUTPUT nv12 surface(memory) |
| | 4839 | | // prefer hwmap to hwdownload on opencl/vaapi. |
| 0 | 4840 | | mainFilters.Add(isHwmapNotUsable ? "hwdownload" : "hwmap=mode=read"); |
| 0 | 4841 | | mainFilters.Add("format=nv12"); |
| | 4842 | | } |
| | 4843 | |
|
| | 4844 | | // OUTPUT nv12 surface(memory) |
| 0 | 4845 | | if (isSwDecoder && isVaapiEncoder) |
| | 4846 | | { |
| 0 | 4847 | | memoryOutput = true; |
| | 4848 | | } |
| | 4849 | |
|
| 0 | 4850 | | if (memoryOutput) |
| | 4851 | | { |
| | 4852 | | // text subtitles |
| 0 | 4853 | | if (hasTextSubs) |
| | 4854 | | { |
| 0 | 4855 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 4856 | | mainFilters.Add(textSubtitlesFilter); |
| | 4857 | | } |
| | 4858 | | } |
| | 4859 | |
|
| 0 | 4860 | | if (memoryOutput && isVaapiEncoder) |
| | 4861 | | { |
| 0 | 4862 | | if (!hasGraphicalSubs) |
| | 4863 | | { |
| 0 | 4864 | | mainFilters.Add("hwupload_vaapi"); |
| | 4865 | | } |
| | 4866 | | } |
| | 4867 | |
|
| | 4868 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 4869 | | var subFilters = new List<string>(); |
| 0 | 4870 | | var overlayFilters = new List<string>(); |
| 0 | 4871 | | if (isVaInVaOut) |
| | 4872 | | { |
| 0 | 4873 | | if (hasSubs) |
| | 4874 | | { |
| 0 | 4875 | | if (hasGraphicalSubs) |
| | 4876 | | { |
| | 4877 | | // overlay_vaapi can handle overlay scaling, setup a smaller height to reduce transfer overhead |
| 0 | 4878 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 4879 | | subFilters.Add(subPreProcFilters); |
| 0 | 4880 | | subFilters.Add("format=bgra"); |
| | 4881 | | } |
| 0 | 4882 | | else if (hasTextSubs) |
| | 4883 | | { |
| 0 | 4884 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 4885 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 4886 | |
|
| 0 | 4887 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, 1080, subFram |
| 0 | 4888 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 4889 | | subFilters.Add(alphaSrcFilter); |
| 0 | 4890 | | subFilters.Add("format=bgra"); |
| 0 | 4891 | | subFilters.Add(subTextSubtitlesFilter); |
| | 4892 | | } |
| | 4893 | |
|
| 0 | 4894 | | subFilters.Add("hwupload=derive_device=vaapi"); |
| | 4895 | |
|
| 0 | 4896 | | var (overlayW, overlayH) = GetFixedOutputSize(swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH); |
| 0 | 4897 | | var overlaySize = (overlayW.HasValue && overlayH.HasValue) |
| 0 | 4898 | | ? $":w={overlayW.Value}:h={overlayH.Value}" |
| 0 | 4899 | | : string.Empty; |
| 0 | 4900 | | var overlayVaapiFilter = string.Format( |
| 0 | 4901 | | CultureInfo.InvariantCulture, |
| 0 | 4902 | | "overlay_vaapi=eof_action=pass:repeatlast=0{0}", |
| 0 | 4903 | | overlaySize); |
| 0 | 4904 | | overlayFilters.Add(overlayVaapiFilter); |
| | 4905 | | } |
| | 4906 | | } |
| 0 | 4907 | | else if (memoryOutput) |
| | 4908 | | { |
| 0 | 4909 | | if (hasGraphicalSubs) |
| | 4910 | | { |
| 0 | 4911 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 4912 | | subFilters.Add(subPreProcFilters); |
| 0 | 4913 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 4914 | |
|
| 0 | 4915 | | if (isVaapiEncoder) |
| | 4916 | | { |
| 0 | 4917 | | overlayFilters.Add("hwupload_vaapi"); |
| | 4918 | | } |
| | 4919 | | } |
| | 4920 | | } |
| | 4921 | |
|
| 0 | 4922 | | return (mainFilters, subFilters, overlayFilters); |
| | 4923 | | } |
| | 4924 | |
|
| | 4925 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAmdVaapiFullVidFilter |
| | 4926 | | EncodingJobInfo state, |
| | 4927 | | EncodingOptions options, |
| | 4928 | | string vidDecoder, |
| | 4929 | | string vidEncoder) |
| | 4930 | | { |
| 0 | 4931 | | var inW = state.VideoStream?.Width; |
| 0 | 4932 | | var inH = state.VideoStream?.Height; |
| 0 | 4933 | | var reqW = state.BaseRequest.Width; |
| 0 | 4934 | | var reqH = state.BaseRequest.Height; |
| 0 | 4935 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 4936 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 4937 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 4938 | |
|
| 0 | 4939 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4940 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4941 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4942 | | var isSwEncoder = !isVaapiEncoder; |
| 0 | 4943 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| | 4944 | |
|
| 0 | 4945 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 4946 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 4947 | | var doVkTonemap = IsVulkanHwTonemapAvailable(state, options); |
| 0 | 4948 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| | 4949 | |
|
| 0 | 4950 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 4951 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4952 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4953 | | var hasAssSubs = hasSubs |
| 0 | 4954 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 4955 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| | 4956 | |
|
| 0 | 4957 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 4958 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 4959 | | var doVkTranspose = isVaapiDecoder && !string.IsNullOrEmpty(transposeDir); |
| 0 | 4960 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isVaapiDecoder && doVkTranspose)); |
| 0 | 4961 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 4962 | | var swpInH = swapWAndH ? inW : inH; |
| | 4963 | |
|
| | 4964 | | /* Make main filters for video stream */ |
| 0 | 4965 | | var mainFilters = new List<string>(); |
| | 4966 | |
|
| 0 | 4967 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doVkTonemap)); |
| | 4968 | |
|
| 0 | 4969 | | if (isSwDecoder) |
| | 4970 | | { |
| | 4971 | | // INPUT sw surface(memory) |
| | 4972 | | // sw deint |
| 0 | 4973 | | if (doDeintH2645) |
| | 4974 | | { |
| 0 | 4975 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 4976 | | mainFilters.Add(swDeintFilter); |
| | 4977 | | } |
| | 4978 | |
|
| 0 | 4979 | | if (doVkTonemap || hasSubs) |
| | 4980 | | { |
| | 4981 | | // sw => hw |
| 0 | 4982 | | mainFilters.Add("hwupload=derive_device=vulkan"); |
| 0 | 4983 | | mainFilters.Add("format=vulkan"); |
| | 4984 | | } |
| | 4985 | | else |
| | 4986 | | { |
| | 4987 | | // sw scale |
| 0 | 4988 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, |
| 0 | 4989 | | mainFilters.Add(swScaleFilter); |
| 0 | 4990 | | mainFilters.Add("format=nv12"); |
| | 4991 | | } |
| | 4992 | | } |
| 0 | 4993 | | else if (isVaapiDecoder) |
| | 4994 | | { |
| | 4995 | | // INPUT vaapi surface(vram) |
| 0 | 4996 | | if (doVkTranspose || doVkTonemap || hasSubs) |
| | 4997 | | { |
| | 4998 | | // map from vaapi to vulkan/drm via interop (Polaris/gfx8+). |
| 0 | 4999 | | if (_mediaEncoder.EncoderVersion >= _minFFmpegAlteredVaVkInterop) |
| | 5000 | | { |
| 0 | 5001 | | if (doVkTranspose || !_mediaEncoder.IsVaapiDeviceSupportVulkanDrmModifier) |
| | 5002 | | { |
| | 5003 | | // disable the indirect va-drm-vk mapping since it's no longer reliable. |
| 0 | 5004 | | mainFilters.Add("hwmap=derive_device=drm"); |
| 0 | 5005 | | mainFilters.Add("format=drm_prime"); |
| 0 | 5006 | | mainFilters.Add("hwmap=derive_device=vulkan"); |
| 0 | 5007 | | mainFilters.Add("format=vulkan"); |
| | 5008 | |
|
| | 5009 | | // workaround for libplacebo using the imported vulkan frame on gfx8. |
| 0 | 5010 | | if (!_mediaEncoder.IsVaapiDeviceSupportVulkanDrmModifier) |
| | 5011 | | { |
| 0 | 5012 | | mainFilters.Add("scale_vulkan"); |
| | 5013 | | } |
| | 5014 | | } |
| 0 | 5015 | | else if (doVkTonemap || hasSubs) |
| | 5016 | | { |
| | 5017 | | // non ad-hoc libplacebo also accepts drm_prime direct input. |
| 0 | 5018 | | mainFilters.Add("hwmap=derive_device=drm"); |
| 0 | 5019 | | mainFilters.Add("format=drm_prime"); |
| | 5020 | | } |
| | 5021 | | } |
| | 5022 | | else // legacy va-vk mapping that works only in jellyfin-ffmpeg6 |
| | 5023 | | { |
| 0 | 5024 | | mainFilters.Add("hwmap=derive_device=vulkan"); |
| 0 | 5025 | | mainFilters.Add("format=vulkan"); |
| | 5026 | | } |
| | 5027 | | } |
| | 5028 | | else |
| | 5029 | | { |
| | 5030 | | // hw deint |
| 0 | 5031 | | if (doDeintH2645) |
| | 5032 | | { |
| 0 | 5033 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 5034 | | mainFilters.Add(deintFilter); |
| | 5035 | | } |
| | 5036 | |
|
| | 5037 | | // hw scale |
| 0 | 5038 | | var hwScaleFilter = GetHwScaleFilter("scale", "vaapi", "nv12", false, inW, inH, reqW, reqH, reqMaxW, |
| | 5039 | |
|
| 0 | 5040 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder && !doVkTonemap) |
| | 5041 | | { |
| 0 | 5042 | | hwScaleFilter += ":out_range=pc:mode=hq"; |
| | 5043 | | } |
| | 5044 | |
|
| 0 | 5045 | | mainFilters.Add(hwScaleFilter); |
| | 5046 | | } |
| | 5047 | | } |
| | 5048 | |
|
| | 5049 | | // vk transpose |
| 0 | 5050 | | if (doVkTranspose) |
| | 5051 | | { |
| 0 | 5052 | | if (string.Equals(transposeDir, "reversal", StringComparison.OrdinalIgnoreCase)) |
| | 5053 | | { |
| 0 | 5054 | | mainFilters.Add("flip_vulkan"); |
| | 5055 | | } |
| | 5056 | | else |
| | 5057 | | { |
| 0 | 5058 | | mainFilters.Add($"transpose_vulkan=dir={transposeDir}"); |
| | 5059 | | } |
| | 5060 | | } |
| | 5061 | |
|
| | 5062 | | // vk libplacebo |
| 0 | 5063 | | if (doVkTonemap || hasSubs) |
| | 5064 | | { |
| 0 | 5065 | | var libplaceboFilter = GetLibplaceboFilter(options, "bgra", doVkTonemap, swpInW, swpInH, reqW, reqH, req |
| 0 | 5066 | | mainFilters.Add(libplaceboFilter); |
| 0 | 5067 | | mainFilters.Add("format=vulkan"); |
| | 5068 | | } |
| | 5069 | |
|
| 0 | 5070 | | if (doVkTonemap && !hasSubs) |
| | 5071 | | { |
| | 5072 | | // OUTPUT vaapi(nv12) surface(vram) |
| | 5073 | | // map from vulkan/drm to vaapi via interop (Polaris/gfx8+). |
| 0 | 5074 | | mainFilters.Add("hwmap=derive_device=vaapi"); |
| 0 | 5075 | | mainFilters.Add("format=vaapi"); |
| | 5076 | |
|
| | 5077 | | // clear the surf->meta_offset and output nv12 |
| 0 | 5078 | | mainFilters.Add("scale_vaapi=format=nv12"); |
| | 5079 | |
|
| | 5080 | | // hw deint |
| 0 | 5081 | | if (doDeintH2645) |
| | 5082 | | { |
| 0 | 5083 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 5084 | | mainFilters.Add(deintFilter); |
| | 5085 | | } |
| | 5086 | | } |
| | 5087 | |
|
| 0 | 5088 | | if (!hasSubs) |
| | 5089 | | { |
| | 5090 | | // OUTPUT nv12 surface(memory) |
| 0 | 5091 | | if (isSwEncoder && (doVkTonemap || isVaapiDecoder)) |
| | 5092 | | { |
| 0 | 5093 | | mainFilters.Add("hwdownload"); |
| 0 | 5094 | | mainFilters.Add("format=nv12"); |
| | 5095 | | } |
| | 5096 | |
|
| 0 | 5097 | | if (isSwDecoder && isVaapiEncoder && !doVkTonemap) |
| | 5098 | | { |
| 0 | 5099 | | mainFilters.Add("hwupload_vaapi"); |
| | 5100 | | } |
| | 5101 | | } |
| | 5102 | |
|
| | 5103 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 5104 | | var subFilters = new List<string>(); |
| 0 | 5105 | | var overlayFilters = new List<string>(); |
| 0 | 5106 | | if (hasSubs) |
| | 5107 | | { |
| 0 | 5108 | | if (hasGraphicalSubs) |
| | 5109 | | { |
| 0 | 5110 | | var subW = state.SubtitleStream?.Width; |
| 0 | 5111 | | var subH = state.SubtitleStream?.Height; |
| 0 | 5112 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 5113 | | subFilters.Add(subPreProcFilters); |
| 0 | 5114 | | subFilters.Add("format=bgra"); |
| | 5115 | | } |
| 0 | 5116 | | else if (hasTextSubs) |
| | 5117 | | { |
| 0 | 5118 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 5119 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 5120 | |
|
| 0 | 5121 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subFrame |
| 0 | 5122 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 5123 | | subFilters.Add(alphaSrcFilter); |
| 0 | 5124 | | subFilters.Add("format=bgra"); |
| 0 | 5125 | | subFilters.Add(subTextSubtitlesFilter); |
| | 5126 | | } |
| | 5127 | |
|
| 0 | 5128 | | subFilters.Add("hwupload=derive_device=vulkan"); |
| 0 | 5129 | | subFilters.Add("format=vulkan"); |
| | 5130 | |
|
| 0 | 5131 | | overlayFilters.Add("overlay_vulkan=eof_action=pass:repeatlast=0"); |
| | 5132 | |
|
| 0 | 5133 | | if (isSwEncoder) |
| | 5134 | | { |
| | 5135 | | // OUTPUT nv12 surface(memory) |
| 0 | 5136 | | overlayFilters.Add("scale_vulkan=format=nv12"); |
| 0 | 5137 | | overlayFilters.Add("hwdownload"); |
| 0 | 5138 | | overlayFilters.Add("format=nv12"); |
| | 5139 | | } |
| 0 | 5140 | | else if (isVaapiEncoder) |
| | 5141 | | { |
| | 5142 | | // OUTPUT vaapi(nv12) surface(vram) |
| | 5143 | | // map from vulkan/drm to vaapi via interop (Polaris/gfx8+). |
| 0 | 5144 | | overlayFilters.Add("hwmap=derive_device=vaapi"); |
| 0 | 5145 | | overlayFilters.Add("format=vaapi"); |
| | 5146 | |
|
| | 5147 | | // clear the surf->meta_offset and output nv12 |
| 0 | 5148 | | overlayFilters.Add("scale_vaapi=format=nv12"); |
| | 5149 | |
|
| | 5150 | | // hw deint |
| 0 | 5151 | | if (doDeintH2645) |
| | 5152 | | { |
| 0 | 5153 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 5154 | | overlayFilters.Add(deintFilter); |
| | 5155 | | } |
| | 5156 | | } |
| | 5157 | | } |
| | 5158 | |
|
| 0 | 5159 | | return (mainFilters, subFilters, overlayFilters); |
| | 5160 | | } |
| | 5161 | |
|
| | 5162 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetVaapiLimitedVidFilter |
| | 5163 | | EncodingJobInfo state, |
| | 5164 | | EncodingOptions options, |
| | 5165 | | string vidDecoder, |
| | 5166 | | string vidEncoder) |
| | 5167 | | { |
| 0 | 5168 | | var inW = state.VideoStream?.Width; |
| 0 | 5169 | | var inH = state.VideoStream?.Height; |
| 0 | 5170 | | var reqW = state.BaseRequest.Width; |
| 0 | 5171 | | var reqH = state.BaseRequest.Height; |
| 0 | 5172 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 5173 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 5174 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 5175 | |
|
| 0 | 5176 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 5177 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 5178 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 5179 | | var isSwEncoder = !isVaapiEncoder; |
| 0 | 5180 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 5181 | | var isVaInVaOut = isVaapiDecoder && isVaapiEncoder; |
| 0 | 5182 | | var isi965Driver = _mediaEncoder.IsVaapiDeviceInteli965; |
| 0 | 5183 | | var isAmdDriver = _mediaEncoder.IsVaapiDeviceAmd; |
| | 5184 | |
|
| 0 | 5185 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 5186 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 5187 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 5188 | | var doOclTonemap = IsHwTonemapAvailable(state, options); |
| | 5189 | |
|
| 0 | 5190 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 5191 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5192 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| | 5193 | |
|
| 0 | 5194 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 5195 | | var swapWAndH = Math.Abs(rotation) == 90 && isSwDecoder; |
| 0 | 5196 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 5197 | | var swpInH = swapWAndH ? inW : inH; |
| | 5198 | |
|
| | 5199 | | /* Make main filters for video stream */ |
| 0 | 5200 | | var mainFilters = new List<string>(); |
| | 5201 | |
|
| 0 | 5202 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doOclTonemap)); |
| | 5203 | |
|
| 0 | 5204 | | var outFormat = string.Empty; |
| 0 | 5205 | | if (isSwDecoder) |
| | 5206 | | { |
| | 5207 | | // INPUT sw surface(memory) |
| | 5208 | | // sw deint |
| 0 | 5209 | | if (doDeintH2645) |
| | 5210 | | { |
| 0 | 5211 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 5212 | | mainFilters.Add(swDeintFilter); |
| | 5213 | | } |
| | 5214 | |
|
| 0 | 5215 | | outFormat = doOclTonemap ? "yuv420p10le" : "nv12"; |
| 0 | 5216 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 5217 | | if (isMjpegEncoder && !doOclTonemap) |
| | 5218 | | { |
| | 5219 | | // sw decoder + hw mjpeg encoder |
| 0 | 5220 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 5221 | | } |
| | 5222 | |
|
| | 5223 | | // sw scale |
| 0 | 5224 | | mainFilters.Add(swScaleFilter); |
| 0 | 5225 | | mainFilters.Add("format=" + outFormat); |
| | 5226 | |
|
| | 5227 | | // keep video at memory except ocl tonemap, |
| | 5228 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 5229 | | // sw => hw |
| 0 | 5230 | | if (doOclTonemap) |
| | 5231 | | { |
| 0 | 5232 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 5233 | | } |
| | 5234 | | } |
| 0 | 5235 | | else if (isVaapiDecoder) |
| | 5236 | | { |
| | 5237 | | // INPUT vaapi surface(vram) |
| | 5238 | | // hw deint |
| 0 | 5239 | | if (doDeintH2645) |
| | 5240 | | { |
| 0 | 5241 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 5242 | | mainFilters.Add(deintFilter); |
| | 5243 | | } |
| | 5244 | |
|
| 0 | 5245 | | outFormat = doOclTonemap ? string.Empty : "nv12"; |
| 0 | 5246 | | var hwScaleFilter = GetHwScaleFilter("scale", "vaapi", outFormat, false, inW, inH, reqW, reqH, reqMaxW, |
| | 5247 | |
|
| 0 | 5248 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder) |
| | 5249 | | { |
| 0 | 5250 | | hwScaleFilter += doOclTonemap ? string.Empty : ":out_range=pc"; |
| 0 | 5251 | | hwScaleFilter += ":mode=hq"; |
| | 5252 | | } |
| | 5253 | |
|
| | 5254 | | // allocate extra pool sizes for vaapi vpp |
| 0 | 5255 | | if (!string.IsNullOrEmpty(hwScaleFilter)) |
| | 5256 | | { |
| 0 | 5257 | | hwScaleFilter += ":extra_hw_frames=24"; |
| | 5258 | | } |
| | 5259 | |
|
| | 5260 | | // hw scale |
| 0 | 5261 | | mainFilters.Add(hwScaleFilter); |
| | 5262 | | } |
| | 5263 | |
|
| 0 | 5264 | | if (doOclTonemap && isVaapiDecoder) |
| | 5265 | | { |
| 0 | 5266 | | if (isi965Driver) |
| | 5267 | | { |
| | 5268 | | // map from vaapi to opencl via vaapi-opencl interop(Intel only). |
| 0 | 5269 | | mainFilters.Add("hwmap=derive_device=opencl"); |
| | 5270 | | } |
| | 5271 | | else |
| | 5272 | | { |
| 0 | 5273 | | mainFilters.Add("hwdownload"); |
| 0 | 5274 | | mainFilters.Add("format=p010le"); |
| 0 | 5275 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 5276 | | } |
| | 5277 | | } |
| | 5278 | |
|
| | 5279 | | // ocl tonemap |
| 0 | 5280 | | if (doOclTonemap) |
| | 5281 | | { |
| 0 | 5282 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 5283 | | mainFilters.Add(tonemapFilter); |
| | 5284 | | } |
| | 5285 | |
|
| 0 | 5286 | | if (doOclTonemap && isVaInVaOut) |
| | 5287 | | { |
| 0 | 5288 | | if (isi965Driver) |
| | 5289 | | { |
| | 5290 | | // OUTPUT vaapi(nv12) surface(vram) |
| | 5291 | | // reverse-mapping via vaapi-opencl interop. |
| 0 | 5292 | | mainFilters.Add("hwmap=derive_device=vaapi:reverse=1"); |
| 0 | 5293 | | mainFilters.Add("format=vaapi"); |
| | 5294 | | } |
| | 5295 | | } |
| | 5296 | |
|
| 0 | 5297 | | var memoryOutput = false; |
| 0 | 5298 | | var isUploadForOclTonemap = doOclTonemap && (isSwDecoder || (isVaapiDecoder && !isi965Driver)); |
| 0 | 5299 | | var isHwmapNotUsable = hasGraphicalSubs || isUploadForOclTonemap; |
| 0 | 5300 | | var isHwmapForSubs = hasSubs && isVaapiDecoder; |
| 0 | 5301 | | var isHwUnmapForTextSubs = hasTextSubs && isVaInVaOut && !isUploadForOclTonemap; |
| 0 | 5302 | | if ((isVaapiDecoder && isSwEncoder) || isUploadForOclTonemap || isHwmapForSubs) |
| | 5303 | | { |
| 0 | 5304 | | memoryOutput = true; |
| | 5305 | |
|
| | 5306 | | // OUTPUT nv12 surface(memory) |
| | 5307 | | // prefer hwmap to hwdownload on opencl/vaapi. |
| 0 | 5308 | | mainFilters.Add(isHwmapNotUsable ? "hwdownload" : "hwmap"); |
| 0 | 5309 | | mainFilters.Add("format=nv12"); |
| | 5310 | | } |
| | 5311 | |
|
| | 5312 | | // OUTPUT nv12 surface(memory) |
| 0 | 5313 | | if (isSwDecoder && isVaapiEncoder) |
| | 5314 | | { |
| 0 | 5315 | | memoryOutput = true; |
| | 5316 | | } |
| | 5317 | |
|
| 0 | 5318 | | if (memoryOutput) |
| | 5319 | | { |
| | 5320 | | // text subtitles |
| 0 | 5321 | | if (hasTextSubs) |
| | 5322 | | { |
| 0 | 5323 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 5324 | | mainFilters.Add(textSubtitlesFilter); |
| | 5325 | | } |
| | 5326 | | } |
| | 5327 | |
|
| 0 | 5328 | | if (isHwUnmapForTextSubs) |
| | 5329 | | { |
| 0 | 5330 | | mainFilters.Add("hwmap"); |
| 0 | 5331 | | mainFilters.Add("format=vaapi"); |
| | 5332 | | } |
| 0 | 5333 | | else if (memoryOutput && isVaapiEncoder) |
| | 5334 | | { |
| 0 | 5335 | | if (!hasGraphicalSubs) |
| | 5336 | | { |
| 0 | 5337 | | mainFilters.Add("hwupload_vaapi"); |
| | 5338 | | } |
| | 5339 | | } |
| | 5340 | |
|
| | 5341 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 5342 | | var subFilters = new List<string>(); |
| 0 | 5343 | | var overlayFilters = new List<string>(); |
| 0 | 5344 | | if (memoryOutput) |
| | 5345 | | { |
| 0 | 5346 | | if (hasGraphicalSubs) |
| | 5347 | | { |
| 0 | 5348 | | var subW = state.SubtitleStream?.Width; |
| 0 | 5349 | | var subH = state.SubtitleStream?.Height; |
| 0 | 5350 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 5351 | | subFilters.Add(subPreProcFilters); |
| 0 | 5352 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 5353 | |
|
| 0 | 5354 | | if (isVaapiEncoder) |
| | 5355 | | { |
| 0 | 5356 | | overlayFilters.Add("hwupload_vaapi"); |
| | 5357 | | } |
| | 5358 | | } |
| | 5359 | | } |
| | 5360 | |
|
| 0 | 5361 | | return (mainFilters, subFilters, overlayFilters); |
| | 5362 | | } |
| | 5363 | |
|
| | 5364 | | /// <summary> |
| | 5365 | | /// Gets the parameter of Apple VideoToolBox filter chain. |
| | 5366 | | /// </summary> |
| | 5367 | | /// <param name="state">Encoding state.</param> |
| | 5368 | | /// <param name="options">Encoding options.</param> |
| | 5369 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 5370 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 5371 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAppleVidFilterChain( |
| | 5372 | | EncodingJobInfo state, |
| | 5373 | | EncodingOptions options, |
| | 5374 | | string vidEncoder) |
| | 5375 | | { |
| 0 | 5376 | | if (options.HardwareAccelerationType != HardwareAccelerationType.videotoolbox) |
| | 5377 | | { |
| 0 | 5378 | | return (null, null, null); |
| | 5379 | | } |
| | 5380 | |
|
| | 5381 | | // ReSharper disable once InconsistentNaming |
| 0 | 5382 | | var isMacOS = OperatingSystem.IsMacOS(); |
| 0 | 5383 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 5384 | | var isVtDecoder = vidDecoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 5385 | | var isVtEncoder = vidEncoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 5386 | | var isVtFullSupported = isMacOS && IsVideoToolboxFullSupported(); |
| | 5387 | |
|
| | 5388 | | // legacy videotoolbox pipeline (disable hw filters) |
| 0 | 5389 | | if (!(isVtEncoder || isVtDecoder) |
| 0 | 5390 | | || !isVtFullSupported |
| 0 | 5391 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 5392 | | { |
| 0 | 5393 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 5394 | | } |
| | 5395 | |
|
| | 5396 | | // preferred videotoolbox + metal filters pipeline |
| 0 | 5397 | | return GetAppleVidFiltersPreferred(state, options, vidDecoder, vidEncoder); |
| | 5398 | | } |
| | 5399 | |
|
| | 5400 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAppleVidFiltersPrefer |
| | 5401 | | EncodingJobInfo state, |
| | 5402 | | EncodingOptions options, |
| | 5403 | | string vidDecoder, |
| | 5404 | | string vidEncoder) |
| | 5405 | | { |
| 0 | 5406 | | var isVtEncoder = vidEncoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 5407 | | var isVtDecoder = vidDecoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 5408 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| | 5409 | |
|
| 0 | 5410 | | var inW = state.VideoStream?.Width; |
| 0 | 5411 | | var inH = state.VideoStream?.Height; |
| 0 | 5412 | | var reqW = state.BaseRequest.Width; |
| 0 | 5413 | | var reqH = state.BaseRequest.Height; |
| 0 | 5414 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 5415 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 5416 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 5417 | |
|
| 0 | 5418 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 5419 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 5420 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 5421 | | var doVtTonemap = IsVideoToolboxTonemapAvailable(state, options); |
| 0 | 5422 | | var doMetalTonemap = !doVtTonemap && IsHwTonemapAvailable(state, options); |
| 0 | 5423 | | var usingHwSurface = isVtDecoder && (_mediaEncoder.EncoderVersion >= _minFFmpegWorkingVtHwSurface); |
| | 5424 | |
|
| 0 | 5425 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 5426 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 5427 | | var doVtTranspose = !string.IsNullOrEmpty(transposeDir) && _mediaEncoder.SupportsFilter("transpose_vt"); |
| 0 | 5428 | | var swapWAndH = Math.Abs(rotation) == 90 && doVtTranspose; |
| 0 | 5429 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 5430 | | var swpInH = swapWAndH ? inW : inH; |
| | 5431 | |
|
| 0 | 5432 | | var scaleFormat = string.Empty; |
| | 5433 | | // Use P010 for Metal tone mapping, otherwise force an 8bit output. |
| 0 | 5434 | | if (!string.Equals(state.VideoStream.PixelFormat, "yuv420p", StringComparison.OrdinalIgnoreCase)) |
| | 5435 | | { |
| 0 | 5436 | | if (doMetalTonemap) |
| | 5437 | | { |
| 0 | 5438 | | if (!string.Equals(state.VideoStream.PixelFormat, "yuv420p10le", StringComparison.OrdinalIgnoreCase) |
| | 5439 | | { |
| 0 | 5440 | | scaleFormat = "p010le"; |
| | 5441 | | } |
| | 5442 | | } |
| | 5443 | | else |
| | 5444 | | { |
| 0 | 5445 | | scaleFormat = "nv12"; |
| | 5446 | | } |
| | 5447 | | } |
| | 5448 | |
|
| 0 | 5449 | | var hwScaleFilter = GetHwScaleFilter("scale", "vt", scaleFormat, false, swpInW, swpInH, reqW, reqH, reqMaxW, |
| | 5450 | |
|
| 0 | 5451 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 5452 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5453 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5454 | | var hasAssSubs = hasSubs |
| 0 | 5455 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 5456 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| | 5457 | |
|
| | 5458 | | /* Make main filters for video stream */ |
| 0 | 5459 | | var mainFilters = new List<string>(); |
| | 5460 | |
|
| | 5461 | | // hw deint |
| 0 | 5462 | | if (doDeintH2645) |
| | 5463 | | { |
| 0 | 5464 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "videotoolbox"); |
| 0 | 5465 | | mainFilters.Add(deintFilter); |
| | 5466 | | } |
| | 5467 | |
|
| | 5468 | | // hw transpose |
| 0 | 5469 | | if (doVtTranspose) |
| | 5470 | | { |
| 0 | 5471 | | mainFilters.Add($"transpose_vt=dir={transposeDir}"); |
| | 5472 | | } |
| | 5473 | |
|
| 0 | 5474 | | if (doVtTonemap) |
| | 5475 | | { |
| | 5476 | | const string VtTonemapArgs = "color_matrix=bt709:color_primaries=bt709:color_transfer=bt709"; |
| | 5477 | |
|
| | 5478 | | // scale_vt can handle scaling & tonemapping in one shot, just like vpp_qsv. |
| 0 | 5479 | | hwScaleFilter = string.IsNullOrEmpty(hwScaleFilter) |
| 0 | 5480 | | ? "scale_vt=" + VtTonemapArgs |
| 0 | 5481 | | : hwScaleFilter + ":" + VtTonemapArgs; |
| | 5482 | | } |
| | 5483 | |
|
| | 5484 | | // hw scale & vt tonemap |
| 0 | 5485 | | mainFilters.Add(hwScaleFilter); |
| | 5486 | |
|
| | 5487 | | // Metal tonemap |
| 0 | 5488 | | if (doMetalTonemap) |
| | 5489 | | { |
| 0 | 5490 | | var tonemapFilter = GetHwTonemapFilter(options, "videotoolbox", "nv12", isMjpegEncoder); |
| 0 | 5491 | | mainFilters.Add(tonemapFilter); |
| | 5492 | | } |
| | 5493 | |
|
| | 5494 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 5495 | | var subFilters = new List<string>(); |
| 0 | 5496 | | var overlayFilters = new List<string>(); |
| | 5497 | |
|
| 0 | 5498 | | if (hasSubs) |
| | 5499 | | { |
| 0 | 5500 | | if (hasGraphicalSubs) |
| | 5501 | | { |
| 0 | 5502 | | var subW = state.SubtitleStream?.Width; |
| 0 | 5503 | | var subH = state.SubtitleStream?.Height; |
| 0 | 5504 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 5505 | | subFilters.Add(subPreProcFilters); |
| 0 | 5506 | | subFilters.Add("format=bgra"); |
| | 5507 | | } |
| 0 | 5508 | | else if (hasTextSubs) |
| | 5509 | | { |
| 0 | 5510 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 5511 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 5512 | |
|
| 0 | 5513 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subFrame |
| 0 | 5514 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 5515 | | subFilters.Add(alphaSrcFilter); |
| 0 | 5516 | | subFilters.Add("format=bgra"); |
| 0 | 5517 | | subFilters.Add(subTextSubtitlesFilter); |
| | 5518 | | } |
| | 5519 | |
|
| 0 | 5520 | | subFilters.Add("hwupload"); |
| 0 | 5521 | | overlayFilters.Add("overlay_videotoolbox=eof_action=pass:repeatlast=0"); |
| | 5522 | | } |
| | 5523 | |
|
| 0 | 5524 | | if (usingHwSurface) |
| | 5525 | | { |
| 0 | 5526 | | if (!isVtEncoder) |
| | 5527 | | { |
| 0 | 5528 | | mainFilters.Add("hwdownload"); |
| 0 | 5529 | | mainFilters.Add("format=nv12"); |
| | 5530 | | } |
| | 5531 | |
|
| 0 | 5532 | | return (mainFilters, subFilters, overlayFilters); |
| | 5533 | | } |
| | 5534 | |
|
| | 5535 | | // For old jellyfin-ffmpeg that has broken hwsurface, add a hwupload |
| 0 | 5536 | | var needFiltering = mainFilters.Any(f => !string.IsNullOrEmpty(f)) || |
| 0 | 5537 | | subFilters.Any(f => !string.IsNullOrEmpty(f)) || |
| 0 | 5538 | | overlayFilters.Any(f => !string.IsNullOrEmpty(f)); |
| 0 | 5539 | | if (needFiltering) |
| | 5540 | | { |
| | 5541 | | // INPUT videotoolbox/memory surface(vram/uma) |
| | 5542 | | // this will pass-through automatically if in/out format matches. |
| 0 | 5543 | | mainFilters.Insert(0, "hwupload"); |
| 0 | 5544 | | mainFilters.Insert(0, "format=nv12|p010le|videotoolbox_vld"); |
| | 5545 | |
|
| 0 | 5546 | | if (!isVtEncoder) |
| | 5547 | | { |
| 0 | 5548 | | mainFilters.Add("hwdownload"); |
| 0 | 5549 | | mainFilters.Add("format=nv12"); |
| | 5550 | | } |
| | 5551 | | } |
| | 5552 | |
|
| 0 | 5553 | | return (mainFilters, subFilters, overlayFilters); |
| | 5554 | | } |
| | 5555 | |
|
| | 5556 | | /// <summary> |
| | 5557 | | /// Gets the parameter of Rockchip RKMPP/RKRGA filter chain. |
| | 5558 | | /// </summary> |
| | 5559 | | /// <param name="state">Encoding state.</param> |
| | 5560 | | /// <param name="options">Encoding options.</param> |
| | 5561 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 5562 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 5563 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetRkmppVidFilterChain( |
| | 5564 | | EncodingJobInfo state, |
| | 5565 | | EncodingOptions options, |
| | 5566 | | string vidEncoder) |
| | 5567 | | { |
| 0 | 5568 | | if (options.HardwareAccelerationType != HardwareAccelerationType.rkmpp) |
| | 5569 | | { |
| 0 | 5570 | | return (null, null, null); |
| | 5571 | | } |
| | 5572 | |
|
| 0 | 5573 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 5574 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 5575 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 5576 | | var isSwEncoder = !vidEncoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 5577 | | var isRkmppOclSupported = isLinux && IsRkmppFullSupported() && IsOpenclFullSupported(); |
| | 5578 | |
|
| 0 | 5579 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 5580 | | || !isRkmppOclSupported |
| 0 | 5581 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 5582 | | { |
| 0 | 5583 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 5584 | | } |
| | 5585 | |
|
| | 5586 | | // preferred rkmpp + rkrga + opencl filters pipeline |
| 0 | 5587 | | if (isRkmppOclSupported) |
| | 5588 | | { |
| 0 | 5589 | | return GetRkmppVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 5590 | | } |
| | 5591 | |
|
| 0 | 5592 | | return (null, null, null); |
| | 5593 | | } |
| | 5594 | |
|
| | 5595 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetRkmppVidFiltersPrefer |
| | 5596 | | EncodingJobInfo state, |
| | 5597 | | EncodingOptions options, |
| | 5598 | | string vidDecoder, |
| | 5599 | | string vidEncoder) |
| | 5600 | | { |
| 0 | 5601 | | var inW = state.VideoStream?.Width; |
| 0 | 5602 | | var inH = state.VideoStream?.Height; |
| 0 | 5603 | | var reqW = state.BaseRequest.Width; |
| 0 | 5604 | | var reqH = state.BaseRequest.Height; |
| 0 | 5605 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 5606 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 5607 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 5608 | |
|
| 0 | 5609 | | var isRkmppDecoder = vidDecoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 5610 | | var isRkmppEncoder = vidEncoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 5611 | | var isSwDecoder = !isRkmppDecoder; |
| 0 | 5612 | | var isSwEncoder = !isRkmppEncoder; |
| 0 | 5613 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 5614 | | var isDrmInDrmOut = isRkmppDecoder && isRkmppEncoder; |
| 0 | 5615 | | var isEncoderSupportAfbc = isRkmppEncoder |
| 0 | 5616 | | && (vidEncoder.Contains("h264", StringComparison.OrdinalIgnoreCase) |
| 0 | 5617 | | || vidEncoder.Contains("hevc", StringComparison.OrdinalIgnoreCase)); |
| | 5618 | |
|
| 0 | 5619 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 5620 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 5621 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 5622 | | var doOclTonemap = IsHwTonemapAvailable(state, options); |
| | 5623 | |
|
| 0 | 5624 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 5625 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5626 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5627 | | var hasAssSubs = hasSubs |
| 0 | 5628 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 5629 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 5630 | | var subW = state.SubtitleStream?.Width; |
| 0 | 5631 | | var subH = state.SubtitleStream?.Height; |
| | 5632 | |
|
| 0 | 5633 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 5634 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 5635 | | var doRkVppTranspose = !string.IsNullOrEmpty(transposeDir); |
| 0 | 5636 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isRkmppDecoder && doRkVppTranspose)); |
| 0 | 5637 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 5638 | | var swpInH = swapWAndH ? inW : inH; |
| | 5639 | |
|
| | 5640 | | /* Make main filters for video stream */ |
| 0 | 5641 | | var mainFilters = new List<string>(); |
| | 5642 | |
|
| 0 | 5643 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doOclTonemap)); |
| | 5644 | |
|
| 0 | 5645 | | if (isSwDecoder) |
| | 5646 | | { |
| | 5647 | | // INPUT sw surface(memory) |
| | 5648 | | // sw deint |
| 0 | 5649 | | if (doDeintH2645) |
| | 5650 | | { |
| 0 | 5651 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 5652 | | mainFilters.Add(swDeintFilter); |
| | 5653 | | } |
| | 5654 | |
|
| 0 | 5655 | | var outFormat = doOclTonemap ? "yuv420p10le" : (hasGraphicalSubs ? "yuv420p" : "nv12"); |
| 0 | 5656 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 5657 | | if (isMjpegEncoder && !doOclTonemap) |
| | 5658 | | { |
| | 5659 | | // sw decoder + hw mjpeg encoder |
| 0 | 5660 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 5661 | | } |
| | 5662 | |
|
| 0 | 5663 | | if (!string.IsNullOrEmpty(swScaleFilter)) |
| | 5664 | | { |
| 0 | 5665 | | swScaleFilter += ":flags=fast_bilinear"; |
| | 5666 | | } |
| | 5667 | |
|
| | 5668 | | // sw scale |
| 0 | 5669 | | mainFilters.Add(swScaleFilter); |
| 0 | 5670 | | mainFilters.Add($"format={outFormat}"); |
| | 5671 | |
|
| | 5672 | | // keep video at memory except ocl tonemap, |
| | 5673 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 5674 | | // sw => hw |
| 0 | 5675 | | if (doOclTonemap) |
| | 5676 | | { |
| 0 | 5677 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 5678 | | } |
| | 5679 | | } |
| 0 | 5680 | | else if (isRkmppDecoder) |
| | 5681 | | { |
| | 5682 | | // INPUT rkmpp/drm surface(gem/dma-heap) |
| | 5683 | |
|
| 0 | 5684 | | var isFullAfbcPipeline = isEncoderSupportAfbc && isDrmInDrmOut && !doOclTonemap; |
| 0 | 5685 | | var swapOutputWandH = doRkVppTranspose && swapWAndH; |
| 0 | 5686 | | var outFormat = doOclTonemap ? "p010" : (isMjpegEncoder ? "bgra" : "nv12"); // RGA only support full ran |
| 0 | 5687 | | var hwScaleFilter = GetHwScaleFilter("vpp", "rkrga", outFormat, swapOutputWandH, swpInW, swpInH, reqW, r |
| 0 | 5688 | | var doScaling = GetHwScaleFilter("vpp", "rkrga", string.Empty, swapOutputWandH, swpInW, swpInH, reqW, re |
| | 5689 | |
|
| 0 | 5690 | | if (!hasSubs |
| 0 | 5691 | | || doRkVppTranspose |
| 0 | 5692 | | || !isFullAfbcPipeline |
| 0 | 5693 | | || !string.IsNullOrEmpty(doScaling)) |
| | 5694 | | { |
| | 5695 | | // RGA3 hardware only support (1/8 ~ 8) scaling in each blit operation, |
| | 5696 | | // but in Trickplay there's a case: (3840/320 == 12), enable 2pass for it |
| 0 | 5697 | | if (!string.IsNullOrEmpty(doScaling) |
| 0 | 5698 | | && !IsScaleRatioSupported(inW, inH, reqW, reqH, reqMaxW, reqMaxH, 8.0f)) |
| | 5699 | | { |
| | 5700 | | // Vendor provided BSP kernel has an RGA driver bug that causes the output to be corrupted for P |
| | 5701 | | // Use NV15 instead of P010 to avoid the issue. |
| | 5702 | | // SDR inputs are using BGRA formats already which is not affected. |
| 0 | 5703 | | var intermediateFormat = string.Equals(outFormat, "p010", StringComparison.OrdinalIgnoreCase) ? |
| 0 | 5704 | | var hwScaleFilterFirstPass = $"scale_rkrga=w=iw/7.9:h=ih/7.9:format={intermediateFormat}:force_d |
| 0 | 5705 | | mainFilters.Add(hwScaleFilterFirstPass); |
| | 5706 | | } |
| | 5707 | |
|
| 0 | 5708 | | if (!string.IsNullOrEmpty(hwScaleFilter) && doRkVppTranspose) |
| | 5709 | | { |
| 0 | 5710 | | hwScaleFilter += $":transpose={transposeDir}"; |
| | 5711 | | } |
| | 5712 | |
|
| | 5713 | | // try enabling AFBC to save DDR bandwidth |
| 0 | 5714 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isFullAfbcPipeline) |
| | 5715 | | { |
| 0 | 5716 | | hwScaleFilter += ":afbc=1"; |
| | 5717 | | } |
| | 5718 | |
|
| | 5719 | | // hw transpose & scale |
| 0 | 5720 | | mainFilters.Add(hwScaleFilter); |
| | 5721 | | } |
| | 5722 | | } |
| | 5723 | |
|
| 0 | 5724 | | if (doOclTonemap && isRkmppDecoder) |
| | 5725 | | { |
| | 5726 | | // map from rkmpp/drm to opencl via drm-opencl interop. |
| 0 | 5727 | | mainFilters.Add("hwmap=derive_device=opencl"); |
| | 5728 | | } |
| | 5729 | |
|
| | 5730 | | // ocl tonemap |
| 0 | 5731 | | if (doOclTonemap) |
| | 5732 | | { |
| 0 | 5733 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 5734 | | mainFilters.Add(tonemapFilter); |
| | 5735 | | } |
| | 5736 | |
|
| 0 | 5737 | | var memoryOutput = false; |
| 0 | 5738 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 5739 | | if ((isRkmppDecoder && isSwEncoder) || isUploadForOclTonemap) |
| | 5740 | | { |
| 0 | 5741 | | memoryOutput = true; |
| | 5742 | |
|
| | 5743 | | // OUTPUT nv12 surface(memory) |
| 0 | 5744 | | mainFilters.Add("hwdownload"); |
| 0 | 5745 | | mainFilters.Add("format=nv12"); |
| | 5746 | | } |
| | 5747 | |
|
| | 5748 | | // OUTPUT nv12 surface(memory) |
| 0 | 5749 | | if (isSwDecoder && isRkmppEncoder) |
| | 5750 | | { |
| 0 | 5751 | | memoryOutput = true; |
| | 5752 | | } |
| | 5753 | |
|
| 0 | 5754 | | if (memoryOutput) |
| | 5755 | | { |
| | 5756 | | // text subtitles |
| 0 | 5757 | | if (hasTextSubs) |
| | 5758 | | { |
| 0 | 5759 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 5760 | | mainFilters.Add(textSubtitlesFilter); |
| | 5761 | | } |
| | 5762 | | } |
| | 5763 | |
|
| 0 | 5764 | | if (isDrmInDrmOut) |
| | 5765 | | { |
| 0 | 5766 | | if (doOclTonemap) |
| | 5767 | | { |
| | 5768 | | // OUTPUT drm(nv12) surface(gem/dma-heap) |
| | 5769 | | // reverse-mapping via drm-opencl interop. |
| 0 | 5770 | | mainFilters.Add("hwmap=derive_device=rkmpp:reverse=1"); |
| 0 | 5771 | | mainFilters.Add("format=drm_prime"); |
| | 5772 | | } |
| | 5773 | | } |
| | 5774 | |
|
| | 5775 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 5776 | | var subFilters = new List<string>(); |
| 0 | 5777 | | var overlayFilters = new List<string>(); |
| 0 | 5778 | | if (isDrmInDrmOut) |
| | 5779 | | { |
| 0 | 5780 | | if (hasSubs) |
| | 5781 | | { |
| 0 | 5782 | | if (hasGraphicalSubs) |
| | 5783 | | { |
| 0 | 5784 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 5785 | | subFilters.Add(subPreProcFilters); |
| 0 | 5786 | | subFilters.Add("format=bgra"); |
| | 5787 | | } |
| 0 | 5788 | | else if (hasTextSubs) |
| | 5789 | | { |
| 0 | 5790 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 5791 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 5792 | |
|
| | 5793 | | // alphasrc=s=1280x720:r=10:start=0,format=bgra,subtitles,hwupload |
| 0 | 5794 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subF |
| 0 | 5795 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 5796 | | subFilters.Add(alphaSrcFilter); |
| 0 | 5797 | | subFilters.Add("format=bgra"); |
| 0 | 5798 | | subFilters.Add(subTextSubtitlesFilter); |
| | 5799 | | } |
| | 5800 | |
|
| 0 | 5801 | | subFilters.Add("hwupload=derive_device=rkmpp"); |
| | 5802 | |
|
| | 5803 | | // try enabling AFBC to save DDR bandwidth |
| 0 | 5804 | | var hwOverlayFilter = "overlay_rkrga=eof_action=pass:repeatlast=0:format=nv12"; |
| 0 | 5805 | | if (isEncoderSupportAfbc) |
| | 5806 | | { |
| 0 | 5807 | | hwOverlayFilter += ":afbc=1"; |
| | 5808 | | } |
| | 5809 | |
|
| 0 | 5810 | | overlayFilters.Add(hwOverlayFilter); |
| | 5811 | | } |
| | 5812 | | } |
| 0 | 5813 | | else if (memoryOutput) |
| | 5814 | | { |
| 0 | 5815 | | if (hasGraphicalSubs) |
| | 5816 | | { |
| 0 | 5817 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 5818 | | subFilters.Add(subPreProcFilters); |
| 0 | 5819 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 5820 | | } |
| | 5821 | | } |
| | 5822 | |
|
| 0 | 5823 | | return (mainFilters, subFilters, overlayFilters); |
| | 5824 | | } |
| | 5825 | |
|
| | 5826 | | /// <summary> |
| | 5827 | | /// Gets the parameter of video processing filters. |
| | 5828 | | /// </summary> |
| | 5829 | | /// <param name="state">Encoding state.</param> |
| | 5830 | | /// <param name="options">Encoding options.</param> |
| | 5831 | | /// <param name="outputVideoCodec">Video codec to use.</param> |
| | 5832 | | /// <returns>The video processing filters parameter.</returns> |
| | 5833 | | public string GetVideoProcessingFilterParam( |
| | 5834 | | EncodingJobInfo state, |
| | 5835 | | EncodingOptions options, |
| | 5836 | | string outputVideoCodec) |
| | 5837 | | { |
| 0 | 5838 | | var videoStream = state.VideoStream; |
| 0 | 5839 | | if (videoStream is null) |
| | 5840 | | { |
| 0 | 5841 | | return string.Empty; |
| | 5842 | | } |
| | 5843 | |
|
| 0 | 5844 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 5845 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5846 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| | 5847 | |
|
| | 5848 | | List<string> mainFilters; |
| | 5849 | | List<string> subFilters; |
| | 5850 | | List<string> overlayFilters; |
| | 5851 | |
|
| 0 | 5852 | | (mainFilters, subFilters, overlayFilters) = options.HardwareAccelerationType switch |
| 0 | 5853 | | { |
| 0 | 5854 | | HardwareAccelerationType.vaapi => GetVaapiVidFilterChain(state, options, outputVideoCodec), |
| 0 | 5855 | | HardwareAccelerationType.amf => GetAmdVidFilterChain(state, options, outputVideoCodec), |
| 0 | 5856 | | HardwareAccelerationType.qsv => GetIntelVidFilterChain(state, options, outputVideoCodec), |
| 0 | 5857 | | HardwareAccelerationType.nvenc => GetNvidiaVidFilterChain(state, options, outputVideoCodec), |
| 0 | 5858 | | HardwareAccelerationType.videotoolbox => GetAppleVidFilterChain(state, options, outputVideoCodec), |
| 0 | 5859 | | HardwareAccelerationType.rkmpp => GetRkmppVidFilterChain(state, options, outputVideoCodec), |
| 0 | 5860 | | _ => GetSwVidFilterChain(state, options, outputVideoCodec), |
| 0 | 5861 | | }; |
| | 5862 | |
|
| 0 | 5863 | | mainFilters?.RemoveAll(string.IsNullOrEmpty); |
| 0 | 5864 | | subFilters?.RemoveAll(string.IsNullOrEmpty); |
| 0 | 5865 | | overlayFilters?.RemoveAll(string.IsNullOrEmpty); |
| | 5866 | |
|
| 0 | 5867 | | var framerate = GetFramerateParam(state); |
| 0 | 5868 | | if (framerate.HasValue) |
| | 5869 | | { |
| 0 | 5870 | | mainFilters.Insert(0, string.Format( |
| 0 | 5871 | | CultureInfo.InvariantCulture, |
| 0 | 5872 | | "fps={0}", |
| 0 | 5873 | | framerate.Value)); |
| | 5874 | | } |
| | 5875 | |
|
| 0 | 5876 | | var mainStr = string.Empty; |
| 0 | 5877 | | if (mainFilters?.Count > 0) |
| | 5878 | | { |
| 0 | 5879 | | mainStr = string.Format( |
| 0 | 5880 | | CultureInfo.InvariantCulture, |
| 0 | 5881 | | "{0}", |
| 0 | 5882 | | string.Join(',', mainFilters)); |
| | 5883 | | } |
| | 5884 | |
|
| 0 | 5885 | | if (overlayFilters?.Count == 0) |
| | 5886 | | { |
| | 5887 | | // -vf "scale..." |
| 0 | 5888 | | return string.IsNullOrEmpty(mainStr) ? string.Empty : " -vf \"" + mainStr + "\""; |
| | 5889 | | } |
| | 5890 | |
|
| 0 | 5891 | | if (overlayFilters?.Count > 0 |
| 0 | 5892 | | && subFilters?.Count > 0 |
| 0 | 5893 | | && state.SubtitleStream is not null) |
| | 5894 | | { |
| | 5895 | | // overlay graphical/text subtitles |
| 0 | 5896 | | var subStr = string.Format( |
| 0 | 5897 | | CultureInfo.InvariantCulture, |
| 0 | 5898 | | "{0}", |
| 0 | 5899 | | string.Join(',', subFilters)); |
| | 5900 | |
|
| 0 | 5901 | | var overlayStr = string.Format( |
| 0 | 5902 | | CultureInfo.InvariantCulture, |
| 0 | 5903 | | "{0}", |
| 0 | 5904 | | string.Join(',', overlayFilters)); |
| | 5905 | |
|
| 0 | 5906 | | var mapPrefix = Convert.ToInt32(state.SubtitleStream.IsExternal); |
| 0 | 5907 | | var subtitleStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.SubtitleStream); |
| 0 | 5908 | | var videoStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.VideoStream); |
| | 5909 | |
|
| 0 | 5910 | | if (hasSubs) |
| | 5911 | | { |
| | 5912 | | // -filter_complex "[0:s]scale=s[sub]..." |
| 0 | 5913 | | var filterStr = string.IsNullOrEmpty(mainStr) |
| 0 | 5914 | | ? " -filter_complex \"[{0}:{1}]{4}[sub];[0:{2}][sub]{5}\"" |
| 0 | 5915 | | : " -filter_complex \"[{0}:{1}]{4}[sub];[0:{2}]{3}[main];[main][sub]{5}\""; |
| | 5916 | |
|
| 0 | 5917 | | if (hasTextSubs) |
| | 5918 | | { |
| 0 | 5919 | | filterStr = string.IsNullOrEmpty(mainStr) |
| 0 | 5920 | | ? " -filter_complex \"{4}[sub];[0:{2}][sub]{5}\"" |
| 0 | 5921 | | : " -filter_complex \"{4}[sub];[0:{2}]{3}[main];[main][sub]{5}\""; |
| | 5922 | | } |
| | 5923 | |
|
| 0 | 5924 | | return string.Format( |
| 0 | 5925 | | CultureInfo.InvariantCulture, |
| 0 | 5926 | | filterStr, |
| 0 | 5927 | | mapPrefix, |
| 0 | 5928 | | subtitleStreamIndex, |
| 0 | 5929 | | videoStreamIndex, |
| 0 | 5930 | | mainStr, |
| 0 | 5931 | | subStr, |
| 0 | 5932 | | overlayStr); |
| | 5933 | | } |
| | 5934 | | } |
| | 5935 | |
|
| 0 | 5936 | | return string.Empty; |
| | 5937 | | } |
| | 5938 | |
|
| | 5939 | | public string GetOverwriteColorPropertiesParam(EncodingJobInfo state, bool isTonemapAvailable) |
| | 5940 | | { |
| 0 | 5941 | | if (isTonemapAvailable) |
| | 5942 | | { |
| 0 | 5943 | | return GetInputHdrParam(state.VideoStream?.ColorTransfer); |
| | 5944 | | } |
| | 5945 | |
|
| 0 | 5946 | | return GetOutputSdrParam(null); |
| | 5947 | | } |
| | 5948 | |
|
| | 5949 | | public string GetInputHdrParam(string colorTransfer) |
| | 5950 | | { |
| 0 | 5951 | | if (string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase)) |
| | 5952 | | { |
| | 5953 | | // HLG |
| 0 | 5954 | | return "setparams=color_primaries=bt2020:color_trc=arib-std-b67:colorspace=bt2020nc"; |
| | 5955 | | } |
| | 5956 | |
|
| | 5957 | | // HDR10 |
| 0 | 5958 | | return "setparams=color_primaries=bt2020:color_trc=smpte2084:colorspace=bt2020nc"; |
| | 5959 | | } |
| | 5960 | |
|
| | 5961 | | public string GetOutputSdrParam(string tonemappingRange) |
| | 5962 | | { |
| | 5963 | | // SDR |
| 0 | 5964 | | if (string.Equals(tonemappingRange, "tv", StringComparison.OrdinalIgnoreCase)) |
| | 5965 | | { |
| 0 | 5966 | | return "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709:range=tv"; |
| | 5967 | | } |
| | 5968 | |
|
| 0 | 5969 | | if (string.Equals(tonemappingRange, "pc", StringComparison.OrdinalIgnoreCase)) |
| | 5970 | | { |
| 0 | 5971 | | return "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709:range=pc"; |
| | 5972 | | } |
| | 5973 | |
|
| 0 | 5974 | | return "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709"; |
| | 5975 | | } |
| | 5976 | |
|
| | 5977 | | public static int GetVideoColorBitDepth(EncodingJobInfo state) |
| | 5978 | | { |
| 0 | 5979 | | var videoStream = state.VideoStream; |
| 0 | 5980 | | if (videoStream is not null) |
| | 5981 | | { |
| 0 | 5982 | | if (videoStream.BitDepth.HasValue) |
| | 5983 | | { |
| 0 | 5984 | | return videoStream.BitDepth.Value; |
| | 5985 | | } |
| | 5986 | |
|
| 0 | 5987 | | if (string.Equals(videoStream.PixelFormat, "yuv420p", StringComparison.OrdinalIgnoreCase) |
| 0 | 5988 | | || string.Equals(videoStream.PixelFormat, "yuvj420p", StringComparison.OrdinalIgnoreCase) |
| 0 | 5989 | | || string.Equals(videoStream.PixelFormat, "yuv422p", StringComparison.OrdinalIgnoreCase) |
| 0 | 5990 | | || string.Equals(videoStream.PixelFormat, "yuv444p", StringComparison.OrdinalIgnoreCase)) |
| | 5991 | | { |
| 0 | 5992 | | return 8; |
| | 5993 | | } |
| | 5994 | |
|
| 0 | 5995 | | if (string.Equals(videoStream.PixelFormat, "yuv420p10le", StringComparison.OrdinalIgnoreCase) |
| 0 | 5996 | | || string.Equals(videoStream.PixelFormat, "yuv422p10le", StringComparison.OrdinalIgnoreCase) |
| 0 | 5997 | | || string.Equals(videoStream.PixelFormat, "yuv444p10le", StringComparison.OrdinalIgnoreCase)) |
| | 5998 | | { |
| 0 | 5999 | | return 10; |
| | 6000 | | } |
| | 6001 | |
|
| 0 | 6002 | | if (string.Equals(videoStream.PixelFormat, "yuv420p12le", StringComparison.OrdinalIgnoreCase) |
| 0 | 6003 | | || string.Equals(videoStream.PixelFormat, "yuv422p12le", StringComparison.OrdinalIgnoreCase) |
| 0 | 6004 | | || string.Equals(videoStream.PixelFormat, "yuv444p12le", StringComparison.OrdinalIgnoreCase)) |
| | 6005 | | { |
| 0 | 6006 | | return 12; |
| | 6007 | | } |
| | 6008 | |
|
| 0 | 6009 | | return 8; |
| | 6010 | | } |
| | 6011 | |
|
| 0 | 6012 | | return 0; |
| | 6013 | | } |
| | 6014 | |
|
| | 6015 | | /// <summary> |
| | 6016 | | /// Gets the ffmpeg option string for the hardware accelerated video decoder. |
| | 6017 | | /// </summary> |
| | 6018 | | /// <param name="state">The encoding job info.</param> |
| | 6019 | | /// <param name="options">The encoding options.</param> |
| | 6020 | | /// <returns>The option string or null if none available.</returns> |
| | 6021 | | protected string GetHardwareVideoDecoder(EncodingJobInfo state, EncodingOptions options) |
| | 6022 | | { |
| 0 | 6023 | | var videoStream = state.VideoStream; |
| 0 | 6024 | | var mediaSource = state.MediaSource; |
| 0 | 6025 | | if (videoStream is null || mediaSource is null) |
| | 6026 | | { |
| 0 | 6027 | | return null; |
| | 6028 | | } |
| | 6029 | |
|
| | 6030 | | // HWA decoders can handle both video files and video folders. |
| 0 | 6031 | | var videoType = state.VideoType; |
| 0 | 6032 | | if (videoType != VideoType.VideoFile |
| 0 | 6033 | | && videoType != VideoType.Iso |
| 0 | 6034 | | && videoType != VideoType.Dvd |
| 0 | 6035 | | && videoType != VideoType.BluRay) |
| | 6036 | | { |
| 0 | 6037 | | return null; |
| | 6038 | | } |
| | 6039 | |
|
| 0 | 6040 | | if (IsCopyCodec(state.OutputVideoCodec)) |
| | 6041 | | { |
| 0 | 6042 | | return null; |
| | 6043 | | } |
| | 6044 | |
|
| 0 | 6045 | | var hardwareAccelerationType = options.HardwareAccelerationType; |
| | 6046 | |
|
| 0 | 6047 | | if (!string.IsNullOrEmpty(videoStream.Codec) && hardwareAccelerationType != HardwareAccelerationType.none) |
| | 6048 | | { |
| 0 | 6049 | | var bitDepth = GetVideoColorBitDepth(state); |
| | 6050 | |
|
| | 6051 | | // Only HEVC, VP9 and AV1 formats have 10-bit hardware decoder support for most platforms |
| 0 | 6052 | | if (bitDepth == 10 |
| 0 | 6053 | | && !(string.Equals(videoStream.Codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6054 | | || string.Equals(videoStream.Codec, "h265", StringComparison.OrdinalIgnoreCase) |
| 0 | 6055 | | || string.Equals(videoStream.Codec, "vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6056 | | || string.Equals(videoStream.Codec, "av1", StringComparison.OrdinalIgnoreCase))) |
| | 6057 | | { |
| | 6058 | | // RKMPP has H.264 Hi10P decoder |
| 0 | 6059 | | bool hasHardwareHi10P = hardwareAccelerationType == HardwareAccelerationType.rkmpp; |
| | 6060 | |
|
| | 6061 | | // VideoToolbox on Apple Silicon has H.264 Hi10P mode enabled after macOS 14.6 |
| 0 | 6062 | | if (hardwareAccelerationType == HardwareAccelerationType.videotoolbox) |
| | 6063 | | { |
| 0 | 6064 | | var ver = Environment.OSVersion.Version; |
| 0 | 6065 | | var arch = RuntimeInformation.OSArchitecture; |
| 0 | 6066 | | if (arch.Equals(Architecture.Arm64) && ver >= new Version(14, 6)) |
| | 6067 | | { |
| 0 | 6068 | | hasHardwareHi10P = true; |
| | 6069 | | } |
| | 6070 | | } |
| | 6071 | |
|
| 0 | 6072 | | if (!hasHardwareHi10P |
| 0 | 6073 | | && string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 6074 | | { |
| 0 | 6075 | | return null; |
| | 6076 | | } |
| | 6077 | | } |
| | 6078 | |
|
| 0 | 6079 | | var decoder = hardwareAccelerationType switch |
| 0 | 6080 | | { |
| 0 | 6081 | | HardwareAccelerationType.vaapi => GetVaapiVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6082 | | HardwareAccelerationType.amf => GetAmfVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6083 | | HardwareAccelerationType.qsv => GetQsvHwVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6084 | | HardwareAccelerationType.nvenc => GetNvdecVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6085 | | HardwareAccelerationType.videotoolbox => GetVideotoolboxVidDecoder(state, options, videoStream, bitD |
| 0 | 6086 | | HardwareAccelerationType.rkmpp => GetRkmppVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6087 | | _ => string.Empty |
| 0 | 6088 | | }; |
| | 6089 | |
|
| 0 | 6090 | | if (!string.IsNullOrEmpty(decoder)) |
| | 6091 | | { |
| 0 | 6092 | | return decoder; |
| | 6093 | | } |
| | 6094 | | } |
| | 6095 | |
|
| | 6096 | | // leave blank so ffmpeg will decide |
| 0 | 6097 | | return null; |
| | 6098 | | } |
| | 6099 | |
|
| | 6100 | | /// <summary> |
| | 6101 | | /// Gets a hw decoder name. |
| | 6102 | | /// </summary> |
| | 6103 | | /// <param name="options">Encoding options.</param> |
| | 6104 | | /// <param name="decoderPrefix">Decoder prefix.</param> |
| | 6105 | | /// <param name="decoderSuffix">Decoder suffix.</param> |
| | 6106 | | /// <param name="videoCodec">Video codec to use.</param> |
| | 6107 | | /// <param name="bitDepth">Video color bit depth.</param> |
| | 6108 | | /// <returns>Hardware decoder name.</returns> |
| | 6109 | | public string GetHwDecoderName(EncodingOptions options, string decoderPrefix, string decoderSuffix, string video |
| | 6110 | | { |
| 0 | 6111 | | if (string.IsNullOrEmpty(decoderPrefix) || string.IsNullOrEmpty(decoderSuffix)) |
| | 6112 | | { |
| 0 | 6113 | | return null; |
| | 6114 | | } |
| | 6115 | |
|
| 0 | 6116 | | var decoderName = decoderPrefix + '_' + decoderSuffix; |
| | 6117 | |
|
| 0 | 6118 | | var isCodecAvailable = _mediaEncoder.SupportsDecoder(decoderName) && options.HardwareDecodingCodecs.Contains |
| | 6119 | |
|
| | 6120 | | // VideoToolbox decoders have built-in SW fallback |
| 0 | 6121 | | if (bitDepth == 10 |
| 0 | 6122 | | && isCodecAvailable |
| 0 | 6123 | | && (options.HardwareAccelerationType != HardwareAccelerationType.videotoolbox)) |
| | 6124 | | { |
| 0 | 6125 | | if (string.Equals(videoCodec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6126 | | && options.HardwareDecodingCodecs.Contains("hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6127 | | && !options.EnableDecodingColorDepth10Hevc) |
| | 6128 | | { |
| 0 | 6129 | | return null; |
| | 6130 | | } |
| | 6131 | |
|
| 0 | 6132 | | if (string.Equals(videoCodec, "vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6133 | | && options.HardwareDecodingCodecs.Contains("vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6134 | | && !options.EnableDecodingColorDepth10Vp9) |
| | 6135 | | { |
| 0 | 6136 | | return null; |
| | 6137 | | } |
| | 6138 | | } |
| | 6139 | |
|
| 0 | 6140 | | if (string.Equals(decoderSuffix, "cuvid", StringComparison.OrdinalIgnoreCase) && options.EnableEnhancedNvdec |
| | 6141 | | { |
| 0 | 6142 | | return null; |
| | 6143 | | } |
| | 6144 | |
|
| 0 | 6145 | | if (string.Equals(decoderSuffix, "qsv", StringComparison.OrdinalIgnoreCase) && options.PreferSystemNativeHwD |
| | 6146 | | { |
| 0 | 6147 | | return null; |
| | 6148 | | } |
| | 6149 | |
|
| 0 | 6150 | | if (string.Equals(decoderSuffix, "rkmpp", StringComparison.OrdinalIgnoreCase)) |
| | 6151 | | { |
| 0 | 6152 | | return null; |
| | 6153 | | } |
| | 6154 | |
|
| 0 | 6155 | | return isCodecAvailable ? (" -c:v " + decoderName) : null; |
| | 6156 | | } |
| | 6157 | |
|
| | 6158 | | /// <summary> |
| | 6159 | | /// Gets a hwaccel type to use as a hardware decoder depending on the system. |
| | 6160 | | /// </summary> |
| | 6161 | | /// <param name="state">Encoding state.</param> |
| | 6162 | | /// <param name="options">Encoding options.</param> |
| | 6163 | | /// <param name="videoCodec">Video codec to use.</param> |
| | 6164 | | /// <param name="bitDepth">Video color bit depth.</param> |
| | 6165 | | /// <param name="outputHwSurface">Specifies if output hw surface.</param> |
| | 6166 | | /// <returns>Hardware accelerator type.</returns> |
| | 6167 | | public string GetHwaccelType(EncodingJobInfo state, EncodingOptions options, string videoCodec, int bitDepth, bo |
| | 6168 | | { |
| 0 | 6169 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 6170 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 6171 | | var isMacOS = OperatingSystem.IsMacOS(); |
| 0 | 6172 | | var isD3d11Supported = isWindows && _mediaEncoder.SupportsHwaccel("d3d11va"); |
| 0 | 6173 | | var isVaapiSupported = isLinux && IsVaapiSupported(state); |
| 0 | 6174 | | var isCudaSupported = (isLinux || isWindows) && IsCudaFullSupported(); |
| 0 | 6175 | | var isQsvSupported = (isLinux || isWindows) && _mediaEncoder.SupportsHwaccel("qsv"); |
| 0 | 6176 | | var isVideotoolboxSupported = isMacOS && _mediaEncoder.SupportsHwaccel("videotoolbox"); |
| 0 | 6177 | | var isRkmppSupported = isLinux && IsRkmppFullSupported(); |
| 0 | 6178 | | var isCodecAvailable = options.HardwareDecodingCodecs.Contains(videoCodec, StringComparison.OrdinalIgnoreCas |
| 0 | 6179 | | var hardwareAccelerationType = options.HardwareAccelerationType; |
| | 6180 | |
|
| 0 | 6181 | | var ffmpegVersion = _mediaEncoder.EncoderVersion; |
| | 6182 | |
|
| | 6183 | | // Set the av1 codec explicitly to trigger hw accelerator, otherwise libdav1d will be used. |
| 0 | 6184 | | var isAv1 = ffmpegVersion < _minFFmpegImplicitHwaccel |
| 0 | 6185 | | && string.Equals(videoCodec, "av1", StringComparison.OrdinalIgnoreCase); |
| | 6186 | |
|
| | 6187 | | // Allow profile mismatch if decoding H.264 baseline with d3d11va and vaapi hwaccels. |
| 0 | 6188 | | var profileMismatch = string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase) |
| 0 | 6189 | | && string.Equals(state.VideoStream?.Profile, "baseline", StringComparison.OrdinalIgnoreCase); |
| | 6190 | |
|
| | 6191 | | // Disable the extra internal copy in nvdec. We already handle it in filter chain. |
| 0 | 6192 | | var nvdecNoInternalCopy = ffmpegVersion >= _minFFmpegHwaUnsafeOutput; |
| | 6193 | |
|
| | 6194 | | // Strip the display rotation side data from the transposed fmp4 output stream. |
| 0 | 6195 | | var stripRotationData = (state.VideoStream?.Rotation ?? 0) != 0 |
| 0 | 6196 | | && ffmpegVersion >= _minFFmpegDisplayRotationOption; |
| 0 | 6197 | | var stripRotationDataArgs = stripRotationData ? " -display_rotation 0" : string.Empty; |
| | 6198 | |
|
| | 6199 | | // VideoToolbox decoders have built-in SW fallback |
| 0 | 6200 | | if (isCodecAvailable |
| 0 | 6201 | | && (options.HardwareAccelerationType != HardwareAccelerationType.videotoolbox)) |
| | 6202 | | { |
| 0 | 6203 | | if (string.Equals(videoCodec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6204 | | && options.HardwareDecodingCodecs.Contains("hevc", StringComparison.OrdinalIgnoreCase)) |
| | 6205 | | { |
| 0 | 6206 | | if (IsVideoStreamHevcRext(state)) |
| | 6207 | | { |
| 0 | 6208 | | if (bitDepth <= 10 && !options.EnableDecodingColorDepth10HevcRext) |
| | 6209 | | { |
| 0 | 6210 | | return null; |
| | 6211 | | } |
| | 6212 | |
|
| 0 | 6213 | | if (bitDepth == 12 && !options.EnableDecodingColorDepth12HevcRext) |
| | 6214 | | { |
| 0 | 6215 | | return null; |
| | 6216 | | } |
| | 6217 | |
|
| 0 | 6218 | | if (hardwareAccelerationType == HardwareAccelerationType.vaapi |
| 0 | 6219 | | && !_mediaEncoder.IsVaapiDeviceInteliHD) |
| | 6220 | | { |
| 0 | 6221 | | return null; |
| | 6222 | | } |
| | 6223 | | } |
| 0 | 6224 | | else if (bitDepth == 10 && !options.EnableDecodingColorDepth10Hevc) |
| | 6225 | | { |
| 0 | 6226 | | return null; |
| | 6227 | | } |
| | 6228 | | } |
| | 6229 | |
|
| 0 | 6230 | | if (string.Equals(videoCodec, "vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6231 | | && options.HardwareDecodingCodecs.Contains("vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6232 | | && bitDepth == 10 |
| 0 | 6233 | | && !options.EnableDecodingColorDepth10Vp9) |
| | 6234 | | { |
| 0 | 6235 | | return null; |
| | 6236 | | } |
| | 6237 | | } |
| | 6238 | |
|
| | 6239 | | // Intel qsv/d3d11va/vaapi |
| 0 | 6240 | | if (hardwareAccelerationType == HardwareAccelerationType.qsv) |
| | 6241 | | { |
| 0 | 6242 | | if (options.PreferSystemNativeHwDecoder) |
| | 6243 | | { |
| 0 | 6244 | | if (isVaapiSupported && isCodecAvailable) |
| | 6245 | | { |
| 0 | 6246 | | return " -hwaccel vaapi" + (outputHwSurface ? " -hwaccel_output_format vaapi -noautorotate" + st |
| 0 | 6247 | | + (profileMismatch ? " -hwaccel_flags +allow_profile_mismatch" : string.Empty) + (isAv1 ? " |
| | 6248 | | } |
| | 6249 | |
|
| 0 | 6250 | | if (isD3d11Supported && isCodecAvailable) |
| | 6251 | | { |
| 0 | 6252 | | return " -hwaccel d3d11va" + (outputHwSurface ? " -hwaccel_output_format d3d11 -noautorotate" + |
| 0 | 6253 | | + (profileMismatch ? " -hwaccel_flags +allow_profile_mismatch" : string.Empty) + " -threads |
| | 6254 | | } |
| | 6255 | | } |
| | 6256 | | else |
| | 6257 | | { |
| 0 | 6258 | | if (isQsvSupported && isCodecAvailable) |
| | 6259 | | { |
| 0 | 6260 | | return " -hwaccel qsv" + (outputHwSurface ? " -hwaccel_output_format qsv -noautorotate" + stripR |
| | 6261 | | } |
| | 6262 | | } |
| | 6263 | | } |
| | 6264 | |
|
| | 6265 | | // Nvidia cuda |
| 0 | 6266 | | if (hardwareAccelerationType == HardwareAccelerationType.nvenc) |
| | 6267 | | { |
| 0 | 6268 | | if (isCudaSupported && isCodecAvailable) |
| | 6269 | | { |
| 0 | 6270 | | if (options.EnableEnhancedNvdecDecoder) |
| | 6271 | | { |
| | 6272 | | // set -threads 1 to nvdec decoder explicitly since it doesn't implement threading support. |
| 0 | 6273 | | return " -hwaccel cuda" + (outputHwSurface ? " -hwaccel_output_format cuda -noautorotate" + stri |
| 0 | 6274 | | + (nvdecNoInternalCopy ? " -hwaccel_flags +unsafe_output" : string.Empty) + " -threads 1" + |
| | 6275 | | } |
| | 6276 | |
|
| | 6277 | | // cuvid decoder doesn't have threading issue. |
| 0 | 6278 | | return " -hwaccel cuda" + (outputHwSurface ? " -hwaccel_output_format cuda -noautorotate" + stripRot |
| | 6279 | | } |
| | 6280 | | } |
| | 6281 | |
|
| | 6282 | | // Amd d3d11va |
| 0 | 6283 | | if (hardwareAccelerationType == HardwareAccelerationType.amf) |
| | 6284 | | { |
| 0 | 6285 | | if (isD3d11Supported && isCodecAvailable) |
| | 6286 | | { |
| 0 | 6287 | | return " -hwaccel d3d11va" + (outputHwSurface ? " -hwaccel_output_format d3d11 -noautorotate" + stri |
| 0 | 6288 | | + (profileMismatch ? " -hwaccel_flags +allow_profile_mismatch" : string.Empty) + (isAv1 ? " -c:v |
| | 6289 | | } |
| | 6290 | | } |
| | 6291 | |
|
| | 6292 | | // Vaapi |
| 0 | 6293 | | if (hardwareAccelerationType == HardwareAccelerationType.vaapi |
| 0 | 6294 | | && isVaapiSupported |
| 0 | 6295 | | && isCodecAvailable) |
| | 6296 | | { |
| 0 | 6297 | | return " -hwaccel vaapi" + (outputHwSurface ? " -hwaccel_output_format vaapi -noautorotate" + stripRotat |
| 0 | 6298 | | + (profileMismatch ? " -hwaccel_flags +allow_profile_mismatch" : string.Empty) + (isAv1 ? " -c:v av1 |
| | 6299 | | } |
| | 6300 | |
|
| | 6301 | | // Apple videotoolbox |
| 0 | 6302 | | if (hardwareAccelerationType == HardwareAccelerationType.videotoolbox |
| 0 | 6303 | | && isVideotoolboxSupported |
| 0 | 6304 | | && isCodecAvailable) |
| | 6305 | | { |
| 0 | 6306 | | return " -hwaccel videotoolbox" + (outputHwSurface ? " -hwaccel_output_format videotoolbox_vld" : string |
| | 6307 | | } |
| | 6308 | |
|
| | 6309 | | // Rockchip rkmpp |
| 0 | 6310 | | if (hardwareAccelerationType == HardwareAccelerationType.rkmpp |
| 0 | 6311 | | && isRkmppSupported |
| 0 | 6312 | | && isCodecAvailable) |
| | 6313 | | { |
| 0 | 6314 | | return " -hwaccel rkmpp" + (outputHwSurface ? " -hwaccel_output_format drm_prime -noautorotate" + stripR |
| | 6315 | | } |
| | 6316 | |
|
| 0 | 6317 | | return null; |
| | 6318 | | } |
| | 6319 | |
|
| | 6320 | | public string GetQsvHwVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bi |
| | 6321 | | { |
| 0 | 6322 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 6323 | | var isLinux = OperatingSystem.IsLinux(); |
| | 6324 | |
|
| 0 | 6325 | | if ((!isWindows && !isLinux) |
| 0 | 6326 | | || options.HardwareAccelerationType != HardwareAccelerationType.qsv) |
| | 6327 | | { |
| 0 | 6328 | | return null; |
| | 6329 | | } |
| | 6330 | |
|
| 0 | 6331 | | var isQsvOclSupported = _mediaEncoder.SupportsHwaccel("qsv") && IsOpenclFullSupported(); |
| 0 | 6332 | | var isIntelDx11OclSupported = isWindows |
| 0 | 6333 | | && _mediaEncoder.SupportsHwaccel("d3d11va") |
| 0 | 6334 | | && isQsvOclSupported; |
| 0 | 6335 | | var isIntelVaapiOclSupported = isLinux |
| 0 | 6336 | | && IsVaapiSupported(state) |
| 0 | 6337 | | && isQsvOclSupported; |
| 0 | 6338 | | var hwSurface = (isIntelDx11OclSupported || isIntelVaapiOclSupported) |
| 0 | 6339 | | && _mediaEncoder.SupportsFilter("alphasrc"); |
| | 6340 | |
|
| 0 | 6341 | | var is8bitSwFormatsQsv = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCas |
| 0 | 6342 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgnor |
| 0 | 6343 | | var is8_10bitSwFormatsQsv = is8bitSwFormatsQsv || string.Equals("yuv420p10le", videoStream.PixelFormat, Stri |
| 0 | 6344 | | var is8_10_12bitSwFormatsQsv = is8_10bitSwFormatsQsv |
| 0 | 6345 | | || string.Equals("yuv422p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6346 | | || string.Equals("yuv444p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6347 | | || string.Equals("yuv422p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6348 | | || string.Equals("yuv444p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6349 | | || string.Equals("yuv420p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6350 | | || string.Equals("yuv422p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6351 | | || string.Equals("yuv444p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase); |
| | 6352 | | // TODO: add more 8/10bit and 4:4:4 formats for Qsv after finishing the ffcheck tool |
| | 6353 | |
|
| 0 | 6354 | | if (is8bitSwFormatsQsv) |
| | 6355 | | { |
| 0 | 6356 | | if (string.Equals(videoStream.Codec, "avc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6357 | | || string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 6358 | | { |
| 0 | 6359 | | return GetHwaccelType(state, options, "h264", bitDepth, hwSurface) + GetHwDecoderName(options, "h264 |
| | 6360 | | } |
| | 6361 | |
|
| 0 | 6362 | | if (string.Equals(videoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase)) |
| | 6363 | | { |
| 0 | 6364 | | return GetHwaccelType(state, options, "vc1", bitDepth, hwSurface) + GetHwDecoderName(options, "vc1", |
| | 6365 | | } |
| | 6366 | |
|
| 0 | 6367 | | if (string.Equals(videoStream.Codec, "vp8", StringComparison.OrdinalIgnoreCase)) |
| | 6368 | | { |
| 0 | 6369 | | return GetHwaccelType(state, options, "vp8", bitDepth, hwSurface) + GetHwDecoderName(options, "vp8", |
| | 6370 | | } |
| | 6371 | |
|
| 0 | 6372 | | if (string.Equals(videoStream.Codec, "mpeg2video", StringComparison.OrdinalIgnoreCase)) |
| | 6373 | | { |
| 0 | 6374 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface) + GetHwDecoderName(options, |
| | 6375 | | } |
| | 6376 | | } |
| | 6377 | |
|
| 0 | 6378 | | if (is8_10bitSwFormatsQsv) |
| | 6379 | | { |
| 0 | 6380 | | if (string.Equals(videoStream.Codec, "vp9", StringComparison.OrdinalIgnoreCase)) |
| | 6381 | | { |
| 0 | 6382 | | return GetHwaccelType(state, options, "vp9", bitDepth, hwSurface) + GetHwDecoderName(options, "vp9", |
| | 6383 | | } |
| | 6384 | |
|
| 0 | 6385 | | if (string.Equals(videoStream.Codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 6386 | | { |
| 0 | 6387 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface) + GetHwDecoderName(options, "av1", |
| | 6388 | | } |
| | 6389 | | } |
| | 6390 | |
|
| 0 | 6391 | | if (is8_10_12bitSwFormatsQsv) |
| | 6392 | | { |
| 0 | 6393 | | if (string.Equals(videoStream.Codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6394 | | || string.Equals(videoStream.Codec, "h265", StringComparison.OrdinalIgnoreCase)) |
| | 6395 | | { |
| 0 | 6396 | | return GetHwaccelType(state, options, "hevc", bitDepth, hwSurface) + GetHwDecoderName(options, "hevc |
| | 6397 | | } |
| | 6398 | | } |
| | 6399 | |
|
| 0 | 6400 | | return null; |
| | 6401 | | } |
| | 6402 | |
|
| | 6403 | | public string GetNvdecVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bi |
| | 6404 | | { |
| 0 | 6405 | | if ((!OperatingSystem.IsWindows() && !OperatingSystem.IsLinux()) |
| 0 | 6406 | | || options.HardwareAccelerationType != HardwareAccelerationType.nvenc) |
| | 6407 | | { |
| 0 | 6408 | | return null; |
| | 6409 | | } |
| | 6410 | |
|
| 0 | 6411 | | var hwSurface = IsCudaFullSupported() && _mediaEncoder.SupportsFilter("alphasrc"); |
| 0 | 6412 | | var is8bitSwFormatsNvdec = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreC |
| 0 | 6413 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgn |
| 0 | 6414 | | var is8_10bitSwFormatsNvdec = is8bitSwFormatsNvdec || string.Equals("yuv420p10le", videoStream.PixelFormat, |
| 0 | 6415 | | var is8_10_12bitSwFormatsNvdec = is8_10bitSwFormatsNvdec |
| 0 | 6416 | | || string.Equals("yuv444p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6417 | | || string.Equals("yuv444p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6418 | | || string.Equals("yuv420p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6419 | | || string.Equals("yuv444p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase); |
| | 6420 | | // TODO: add more 8/10/12bit and 4:4:4 formats for Nvdec after finishing the ffcheck tool |
| | 6421 | |
|
| 0 | 6422 | | if (is8bitSwFormatsNvdec) |
| | 6423 | | { |
| 0 | 6424 | | if (string.Equals("avc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6425 | | || string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6426 | | { |
| 0 | 6427 | | return GetHwaccelType(state, options, "h264", bitDepth, hwSurface) + GetHwDecoderName(options, "h264 |
| | 6428 | | } |
| | 6429 | |
|
| 0 | 6430 | | if (string.Equals("mpeg2video", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6431 | | { |
| 0 | 6432 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface) + GetHwDecoderName(options, |
| | 6433 | | } |
| | 6434 | |
|
| 0 | 6435 | | if (string.Equals("vc1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6436 | | { |
| 0 | 6437 | | return GetHwaccelType(state, options, "vc1", bitDepth, hwSurface) + GetHwDecoderName(options, "vc1", |
| | 6438 | | } |
| | 6439 | |
|
| 0 | 6440 | | if (string.Equals("mpeg4", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6441 | | { |
| 0 | 6442 | | return GetHwaccelType(state, options, "mpeg4", bitDepth, hwSurface) + GetHwDecoderName(options, "mpe |
| | 6443 | | } |
| | 6444 | |
|
| 0 | 6445 | | if (string.Equals("vp8", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6446 | | { |
| 0 | 6447 | | return GetHwaccelType(state, options, "vp8", bitDepth, hwSurface) + GetHwDecoderName(options, "vp8", |
| | 6448 | | } |
| | 6449 | | } |
| | 6450 | |
|
| 0 | 6451 | | if (is8_10bitSwFormatsNvdec) |
| | 6452 | | { |
| 0 | 6453 | | if (string.Equals("vp9", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6454 | | { |
| 0 | 6455 | | return GetHwaccelType(state, options, "vp9", bitDepth, hwSurface) + GetHwDecoderName(options, "vp9", |
| | 6456 | | } |
| | 6457 | |
|
| 0 | 6458 | | if (string.Equals("av1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6459 | | { |
| 0 | 6460 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface) + GetHwDecoderName(options, "av1", |
| | 6461 | | } |
| | 6462 | | } |
| | 6463 | |
|
| 0 | 6464 | | if (is8_10_12bitSwFormatsNvdec) |
| | 6465 | | { |
| 0 | 6466 | | if (string.Equals("hevc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6467 | | || string.Equals("h265", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6468 | | { |
| 0 | 6469 | | return GetHwaccelType(state, options, "hevc", bitDepth, hwSurface) + GetHwDecoderName(options, "hevc |
| | 6470 | | } |
| | 6471 | | } |
| | 6472 | |
|
| 0 | 6473 | | return null; |
| | 6474 | | } |
| | 6475 | |
|
| | 6476 | | public string GetAmfVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bitD |
| | 6477 | | { |
| 0 | 6478 | | if (!OperatingSystem.IsWindows() |
| 0 | 6479 | | || options.HardwareAccelerationType != HardwareAccelerationType.amf) |
| | 6480 | | { |
| 0 | 6481 | | return null; |
| | 6482 | | } |
| | 6483 | |
|
| 0 | 6484 | | var hwSurface = _mediaEncoder.SupportsHwaccel("d3d11va") |
| 0 | 6485 | | && IsOpenclFullSupported() |
| 0 | 6486 | | && _mediaEncoder.SupportsFilter("alphasrc"); |
| 0 | 6487 | | var is8bitSwFormatsAmf = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCas |
| 0 | 6488 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgnor |
| 0 | 6489 | | var is8_10bitSwFormatsAmf = is8bitSwFormatsAmf || string.Equals("yuv420p10le", videoStream.PixelFormat, Stri |
| | 6490 | |
|
| 0 | 6491 | | if (is8bitSwFormatsAmf) |
| | 6492 | | { |
| 0 | 6493 | | if (string.Equals("avc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6494 | | || string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6495 | | { |
| 0 | 6496 | | return GetHwaccelType(state, options, "h264", bitDepth, hwSurface); |
| | 6497 | | } |
| | 6498 | |
|
| 0 | 6499 | | if (string.Equals("mpeg2video", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6500 | | { |
| 0 | 6501 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface); |
| | 6502 | | } |
| | 6503 | |
|
| 0 | 6504 | | if (string.Equals("vc1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6505 | | { |
| 0 | 6506 | | return GetHwaccelType(state, options, "vc1", bitDepth, hwSurface); |
| | 6507 | | } |
| | 6508 | | } |
| | 6509 | |
|
| 0 | 6510 | | if (is8_10bitSwFormatsAmf) |
| | 6511 | | { |
| 0 | 6512 | | if (string.Equals("hevc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6513 | | || string.Equals("h265", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6514 | | { |
| 0 | 6515 | | return GetHwaccelType(state, options, "hevc", bitDepth, hwSurface); |
| | 6516 | | } |
| | 6517 | |
|
| 0 | 6518 | | if (string.Equals("vp9", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6519 | | { |
| 0 | 6520 | | return GetHwaccelType(state, options, "vp9", bitDepth, hwSurface); |
| | 6521 | | } |
| | 6522 | |
|
| 0 | 6523 | | if (string.Equals("av1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6524 | | { |
| 0 | 6525 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface); |
| | 6526 | | } |
| | 6527 | | } |
| | 6528 | |
|
| 0 | 6529 | | return null; |
| | 6530 | | } |
| | 6531 | |
|
| | 6532 | | public string GetVaapiVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bi |
| | 6533 | | { |
| 0 | 6534 | | if (!OperatingSystem.IsLinux() |
| 0 | 6535 | | || options.HardwareAccelerationType != HardwareAccelerationType.vaapi) |
| | 6536 | | { |
| 0 | 6537 | | return null; |
| | 6538 | | } |
| | 6539 | |
|
| 0 | 6540 | | var hwSurface = IsVaapiSupported(state) |
| 0 | 6541 | | && IsVaapiFullSupported() |
| 0 | 6542 | | && IsOpenclFullSupported() |
| 0 | 6543 | | && _mediaEncoder.SupportsFilter("alphasrc"); |
| 0 | 6544 | | var is8bitSwFormatsVaapi = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreC |
| 0 | 6545 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgn |
| 0 | 6546 | | var is8_10bitSwFormatsVaapi = is8bitSwFormatsVaapi || string.Equals("yuv420p10le", videoStream.PixelFormat, |
| 0 | 6547 | | var is8_10_12bitSwFormatsVaapi = is8_10bitSwFormatsVaapi |
| 0 | 6548 | | || string.Equals("yuv422p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6549 | | || string.Equals("yuv444p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6550 | | || string.Equals("yuv422p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6551 | | || string.Equals("yuv444p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6552 | | || string.Equals("yuv420p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6553 | | || string.Equals("yuv422p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6554 | | || string.Equals("yuv444p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase); |
| | 6555 | |
|
| 0 | 6556 | | if (is8bitSwFormatsVaapi) |
| | 6557 | | { |
| 0 | 6558 | | if (string.Equals("avc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6559 | | || string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6560 | | { |
| 0 | 6561 | | return GetHwaccelType(state, options, "h264", bitDepth, hwSurface); |
| | 6562 | | } |
| | 6563 | |
|
| 0 | 6564 | | if (string.Equals("mpeg2video", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6565 | | { |
| 0 | 6566 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface); |
| | 6567 | | } |
| | 6568 | |
|
| 0 | 6569 | | if (string.Equals("vc1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6570 | | { |
| 0 | 6571 | | return GetHwaccelType(state, options, "vc1", bitDepth, hwSurface); |
| | 6572 | | } |
| | 6573 | |
|
| 0 | 6574 | | if (string.Equals("vp8", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6575 | | { |
| 0 | 6576 | | return GetHwaccelType(state, options, "vp8", bitDepth, hwSurface); |
| | 6577 | | } |
| | 6578 | | } |
| | 6579 | |
|
| 0 | 6580 | | if (is8_10bitSwFormatsVaapi) |
| | 6581 | | { |
| 0 | 6582 | | if (string.Equals("vp9", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6583 | | { |
| 0 | 6584 | | return GetHwaccelType(state, options, "vp9", bitDepth, hwSurface); |
| | 6585 | | } |
| | 6586 | |
|
| 0 | 6587 | | if (string.Equals("av1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6588 | | { |
| 0 | 6589 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface); |
| | 6590 | | } |
| | 6591 | | } |
| | 6592 | |
|
| 0 | 6593 | | if (is8_10_12bitSwFormatsVaapi) |
| | 6594 | | { |
| 0 | 6595 | | if (string.Equals("hevc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6596 | | || string.Equals("h265", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6597 | | { |
| 0 | 6598 | | return GetHwaccelType(state, options, "hevc", bitDepth, hwSurface); |
| | 6599 | | } |
| | 6600 | | } |
| | 6601 | |
|
| 0 | 6602 | | return null; |
| | 6603 | | } |
| | 6604 | |
|
| | 6605 | | public string GetVideotoolboxVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, |
| | 6606 | | { |
| 0 | 6607 | | if (!OperatingSystem.IsMacOS() |
| 0 | 6608 | | || options.HardwareAccelerationType != HardwareAccelerationType.videotoolbox) |
| | 6609 | | { |
| 0 | 6610 | | return null; |
| | 6611 | | } |
| | 6612 | |
|
| 0 | 6613 | | var is8bitSwFormatsVt = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase |
| 0 | 6614 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgnore |
| 0 | 6615 | | var is8_10bitSwFormatsVt = is8bitSwFormatsVt || string.Equals("yuv420p10le", videoStream.PixelFormat, String |
| 0 | 6616 | | var is8_10_12bitSwFormatsVt = is8_10bitSwFormatsVt |
| 0 | 6617 | | || string.Equals("yuv422p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6618 | | || string.Equals("yuv444p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6619 | | || string.Equals("yuv422p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6620 | | || string.Equals("yuv444p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6621 | | || string.Equals("yuv420p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6622 | | || string.Equals("yuv422p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6623 | | || string.Equals("yuv444p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase); |
| 0 | 6624 | | var isAv1SupportedSwFormatsVt = is8_10bitSwFormatsVt || string.Equals("yuv420p12le", videoStream.PixelFormat |
| | 6625 | |
|
| | 6626 | | // The related patches make videotoolbox hardware surface working is only available in jellyfin-ffmpeg 7.0.1 |
| 0 | 6627 | | bool useHwSurface = (_mediaEncoder.EncoderVersion >= _minFFmpegWorkingVtHwSurface) && IsVideoToolboxFullSupp |
| | 6628 | |
|
| 0 | 6629 | | if (is8bitSwFormatsVt) |
| | 6630 | | { |
| 0 | 6631 | | if (string.Equals("vp8", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6632 | | { |
| 0 | 6633 | | return GetHwaccelType(state, options, "vp8", bitDepth, useHwSurface); |
| | 6634 | | } |
| | 6635 | | } |
| | 6636 | |
|
| 0 | 6637 | | if (is8_10bitSwFormatsVt) |
| | 6638 | | { |
| 0 | 6639 | | if (string.Equals("avc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6640 | | || string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6641 | | { |
| 0 | 6642 | | return GetHwaccelType(state, options, "h264", bitDepth, useHwSurface); |
| | 6643 | | } |
| | 6644 | |
|
| 0 | 6645 | | if (string.Equals("vp9", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6646 | | { |
| 0 | 6647 | | return GetHwaccelType(state, options, "vp9", bitDepth, useHwSurface); |
| | 6648 | | } |
| | 6649 | | } |
| | 6650 | |
|
| 0 | 6651 | | if (is8_10_12bitSwFormatsVt) |
| | 6652 | | { |
| 0 | 6653 | | if (string.Equals("hevc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6654 | | || string.Equals("h265", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6655 | | { |
| 0 | 6656 | | return GetHwaccelType(state, options, "hevc", bitDepth, useHwSurface); |
| | 6657 | | } |
| | 6658 | |
|
| 0 | 6659 | | if (string.Equals("av1", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6660 | | && isAv1SupportedSwFormatsVt |
| 0 | 6661 | | && _mediaEncoder.IsVideoToolboxAv1DecodeAvailable) |
| | 6662 | | { |
| 0 | 6663 | | return GetHwaccelType(state, options, "av1", bitDepth, useHwSurface); |
| | 6664 | | } |
| | 6665 | | } |
| | 6666 | |
|
| 0 | 6667 | | return null; |
| | 6668 | | } |
| | 6669 | |
|
| | 6670 | | public string GetRkmppVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bi |
| | 6671 | | { |
| 0 | 6672 | | var isLinux = OperatingSystem.IsLinux(); |
| | 6673 | |
|
| 0 | 6674 | | if (!isLinux |
| 0 | 6675 | | || options.HardwareAccelerationType != HardwareAccelerationType.rkmpp) |
| | 6676 | | { |
| 0 | 6677 | | return null; |
| | 6678 | | } |
| | 6679 | |
|
| 0 | 6680 | | var inW = state.VideoStream?.Width; |
| 0 | 6681 | | var inH = state.VideoStream?.Height; |
| 0 | 6682 | | var reqW = state.BaseRequest.Width; |
| 0 | 6683 | | var reqH = state.BaseRequest.Height; |
| 0 | 6684 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 6685 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| | 6686 | |
|
| | 6687 | | // rkrga RGA2e supports range from 1/16 to 16 |
| 0 | 6688 | | if (!IsScaleRatioSupported(inW, inH, reqW, reqH, reqMaxW, reqMaxH, 16.0f)) |
| | 6689 | | { |
| 0 | 6690 | | return null; |
| | 6691 | | } |
| | 6692 | |
|
| 0 | 6693 | | var isRkmppOclSupported = IsRkmppFullSupported() && IsOpenclFullSupported(); |
| 0 | 6694 | | var hwSurface = isRkmppOclSupported |
| 0 | 6695 | | && _mediaEncoder.SupportsFilter("alphasrc"); |
| | 6696 | |
|
| | 6697 | | // rkrga RGA3 supports range from 1/8 to 8 |
| 0 | 6698 | | var isAfbcSupported = hwSurface && IsScaleRatioSupported(inW, inH, reqW, reqH, reqMaxW, reqMaxH, 8.0f); |
| | 6699 | |
|
| | 6700 | | // TODO: add more 8/10bit and 4:2:2 formats for Rkmpp after finishing the ffcheck tool |
| 0 | 6701 | | var is8bitSwFormatsRkmpp = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreC |
| 0 | 6702 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgn |
| 0 | 6703 | | var is10bitSwFormatsRkmpp = string.Equals("yuv420p10le", videoStream.PixelFormat, StringComparison.OrdinalIg |
| 0 | 6704 | | var is8_10bitSwFormatsRkmpp = is8bitSwFormatsRkmpp || is10bitSwFormatsRkmpp; |
| | 6705 | |
|
| | 6706 | | // nv15 and nv20 are bit-stream only formats |
| 0 | 6707 | | if (is10bitSwFormatsRkmpp && !hwSurface) |
| | 6708 | | { |
| 0 | 6709 | | return null; |
| | 6710 | | } |
| | 6711 | |
|
| 0 | 6712 | | if (is8bitSwFormatsRkmpp) |
| | 6713 | | { |
| 0 | 6714 | | if (string.Equals(videoStream.Codec, "mpeg1video", StringComparison.OrdinalIgnoreCase)) |
| | 6715 | | { |
| 0 | 6716 | | return GetHwaccelType(state, options, "mpeg1video", bitDepth, hwSurface); |
| | 6717 | | } |
| | 6718 | |
|
| 0 | 6719 | | if (string.Equals(videoStream.Codec, "mpeg2video", StringComparison.OrdinalIgnoreCase)) |
| | 6720 | | { |
| 0 | 6721 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface); |
| | 6722 | | } |
| | 6723 | |
|
| 0 | 6724 | | if (string.Equals(videoStream.Codec, "mpeg4", StringComparison.OrdinalIgnoreCase)) |
| | 6725 | | { |
| 0 | 6726 | | return GetHwaccelType(state, options, "mpeg4", bitDepth, hwSurface); |
| | 6727 | | } |
| | 6728 | |
|
| 0 | 6729 | | if (string.Equals(videoStream.Codec, "vp8", StringComparison.OrdinalIgnoreCase)) |
| | 6730 | | { |
| 0 | 6731 | | return GetHwaccelType(state, options, "vp8", bitDepth, hwSurface); |
| | 6732 | | } |
| | 6733 | | } |
| | 6734 | |
|
| 0 | 6735 | | if (is8_10bitSwFormatsRkmpp) |
| | 6736 | | { |
| 0 | 6737 | | if (string.Equals(videoStream.Codec, "avc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6738 | | || string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 6739 | | { |
| 0 | 6740 | | var accelType = GetHwaccelType(state, options, "h264", bitDepth, hwSurface); |
| 0 | 6741 | | return accelType + ((!string.IsNullOrEmpty(accelType) && isAfbcSupported) ? " -afbc rga" : string.Em |
| | 6742 | | } |
| | 6743 | |
|
| 0 | 6744 | | if (string.Equals(videoStream.Codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6745 | | || string.Equals(videoStream.Codec, "h265", StringComparison.OrdinalIgnoreCase)) |
| | 6746 | | { |
| 0 | 6747 | | var accelType = GetHwaccelType(state, options, "hevc", bitDepth, hwSurface); |
| 0 | 6748 | | return accelType + ((!string.IsNullOrEmpty(accelType) && isAfbcSupported) ? " -afbc rga" : string.Em |
| | 6749 | | } |
| | 6750 | |
|
| 0 | 6751 | | if (string.Equals(videoStream.Codec, "vp9", StringComparison.OrdinalIgnoreCase)) |
| | 6752 | | { |
| 0 | 6753 | | var accelType = GetHwaccelType(state, options, "vp9", bitDepth, hwSurface); |
| 0 | 6754 | | return accelType + ((!string.IsNullOrEmpty(accelType) && isAfbcSupported) ? " -afbc rga" : string.Em |
| | 6755 | | } |
| | 6756 | |
|
| 0 | 6757 | | if (string.Equals(videoStream.Codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 6758 | | { |
| 0 | 6759 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface); |
| | 6760 | | } |
| | 6761 | | } |
| | 6762 | |
|
| 0 | 6763 | | return null; |
| | 6764 | | } |
| | 6765 | |
|
| | 6766 | | /// <summary> |
| | 6767 | | /// Gets the number of threads. |
| | 6768 | | /// </summary> |
| | 6769 | | /// <param name="state">Encoding state.</param> |
| | 6770 | | /// <param name="encodingOptions">Encoding options.</param> |
| | 6771 | | /// <param name="outputVideoCodec">Video codec to use.</param> |
| | 6772 | | /// <returns>Number of threads.</returns> |
| | 6773 | | #nullable enable |
| | 6774 | | public static int GetNumberOfThreads(EncodingJobInfo? state, EncodingOptions encodingOptions, string? outputVide |
| | 6775 | | { |
| 0 | 6776 | | var threads = state?.BaseRequest.CpuCoreLimit ?? encodingOptions.EncodingThreadCount; |
| | 6777 | |
|
| 0 | 6778 | | if (threads <= 0) |
| | 6779 | | { |
| | 6780 | | // Automatically set thread count |
| 0 | 6781 | | return 0; |
| | 6782 | | } |
| | 6783 | |
|
| 0 | 6784 | | return Math.Min(threads, Environment.ProcessorCount); |
| | 6785 | | } |
| | 6786 | |
|
| | 6787 | | #nullable disable |
| | 6788 | | public void TryStreamCopy(EncodingJobInfo state) |
| | 6789 | | { |
| 0 | 6790 | | if (state.VideoStream is not null && CanStreamCopyVideo(state, state.VideoStream)) |
| | 6791 | | { |
| 0 | 6792 | | state.OutputVideoCodec = "copy"; |
| | 6793 | | } |
| | 6794 | | else |
| | 6795 | | { |
| 0 | 6796 | | var user = state.User; |
| | 6797 | |
|
| | 6798 | | // If the user doesn't have access to transcoding, then force stream copy, regardless of whether it will |
| 0 | 6799 | | if (user is not null && !user.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding)) |
| | 6800 | | { |
| 0 | 6801 | | state.OutputVideoCodec = "copy"; |
| | 6802 | | } |
| | 6803 | | } |
| | 6804 | |
|
| 0 | 6805 | | if (state.AudioStream is not null |
| 0 | 6806 | | && CanStreamCopyAudio(state, state.AudioStream, state.SupportedAudioCodecs)) |
| | 6807 | | { |
| 0 | 6808 | | state.OutputAudioCodec = "copy"; |
| | 6809 | | } |
| | 6810 | | else |
| | 6811 | | { |
| 0 | 6812 | | var user = state.User; |
| | 6813 | |
|
| | 6814 | | // If the user doesn't have access to transcoding, then force stream copy, regardless of whether it will |
| 0 | 6815 | | if (user is not null && !user.HasPermission(PermissionKind.EnableAudioPlaybackTranscoding)) |
| | 6816 | | { |
| 0 | 6817 | | state.OutputAudioCodec = "copy"; |
| | 6818 | | } |
| | 6819 | | } |
| 0 | 6820 | | } |
| | 6821 | |
|
| | 6822 | | public string GetInputModifier(EncodingJobInfo state, EncodingOptions encodingOptions, string segmentContainer) |
| | 6823 | | { |
| 0 | 6824 | | var inputModifier = string.Empty; |
| 0 | 6825 | | var analyzeDurationArgument = string.Empty; |
| | 6826 | |
|
| | 6827 | | // Apply -analyzeduration as per the environment variable, |
| | 6828 | | // otherwise ffmpeg will break on certain files due to default value is 0. |
| 0 | 6829 | | var ffmpegAnalyzeDuration = _config.GetFFmpegAnalyzeDuration() ?? string.Empty; |
| | 6830 | |
|
| 0 | 6831 | | if (state.MediaSource.AnalyzeDurationMs > 0) |
| | 6832 | | { |
| 0 | 6833 | | analyzeDurationArgument = "-analyzeduration " + (state.MediaSource.AnalyzeDurationMs.Value * 1000).ToStr |
| | 6834 | | } |
| 0 | 6835 | | else if (!string.IsNullOrEmpty(ffmpegAnalyzeDuration)) |
| | 6836 | | { |
| 0 | 6837 | | analyzeDurationArgument = "-analyzeduration " + ffmpegAnalyzeDuration; |
| | 6838 | | } |
| | 6839 | |
|
| 0 | 6840 | | if (!string.IsNullOrEmpty(analyzeDurationArgument)) |
| | 6841 | | { |
| 0 | 6842 | | inputModifier += " " + analyzeDurationArgument; |
| | 6843 | | } |
| | 6844 | |
|
| 0 | 6845 | | inputModifier = inputModifier.Trim(); |
| | 6846 | |
|
| | 6847 | | // Apply -probesize if configured |
| 0 | 6848 | | var ffmpegProbeSize = _config.GetFFmpegProbeSize(); |
| | 6849 | |
|
| 0 | 6850 | | if (!string.IsNullOrEmpty(ffmpegProbeSize)) |
| | 6851 | | { |
| 0 | 6852 | | inputModifier += $" -probesize {ffmpegProbeSize}"; |
| | 6853 | | } |
| | 6854 | |
|
| 0 | 6855 | | var userAgentParam = GetUserAgentParam(state); |
| | 6856 | |
|
| 0 | 6857 | | if (!string.IsNullOrEmpty(userAgentParam)) |
| | 6858 | | { |
| 0 | 6859 | | inputModifier += " " + userAgentParam; |
| | 6860 | | } |
| | 6861 | |
|
| 0 | 6862 | | inputModifier = inputModifier.Trim(); |
| | 6863 | |
|
| 0 | 6864 | | var refererParam = GetRefererParam(state); |
| | 6865 | |
|
| 0 | 6866 | | if (!string.IsNullOrEmpty(refererParam)) |
| | 6867 | | { |
| 0 | 6868 | | inputModifier += " " + refererParam; |
| | 6869 | | } |
| | 6870 | |
|
| 0 | 6871 | | inputModifier = inputModifier.Trim(); |
| | 6872 | |
|
| 0 | 6873 | | inputModifier += " " + GetFastSeekCommandLineParameter(state, encodingOptions, segmentContainer); |
| 0 | 6874 | | inputModifier = inputModifier.Trim(); |
| | 6875 | |
|
| 0 | 6876 | | if (state.InputProtocol == MediaProtocol.Rtsp) |
| | 6877 | | { |
| 0 | 6878 | | inputModifier += " -rtsp_transport tcp+udp -rtsp_flags prefer_tcp"; |
| | 6879 | | } |
| | 6880 | |
|
| 0 | 6881 | | if (!string.IsNullOrEmpty(state.InputAudioSync)) |
| | 6882 | | { |
| 0 | 6883 | | inputModifier += " -async " + state.InputAudioSync; |
| | 6884 | | } |
| | 6885 | |
|
| 0 | 6886 | | if (!string.IsNullOrEmpty(state.InputVideoSync)) |
| | 6887 | | { |
| 0 | 6888 | | inputModifier += GetVideoSyncOption(state.InputVideoSync, _mediaEncoder.EncoderVersion); |
| | 6889 | | } |
| | 6890 | |
|
| 0 | 6891 | | if (state.ReadInputAtNativeFramerate && state.InputProtocol != MediaProtocol.Rtsp) |
| | 6892 | | { |
| 0 | 6893 | | inputModifier += " -re"; |
| | 6894 | | } |
| 0 | 6895 | | else if (encodingOptions.EnableSegmentDeletion |
| 0 | 6896 | | && state.VideoStream is not null |
| 0 | 6897 | | && state.TranscodingType == TranscodingJobType.Hls |
| 0 | 6898 | | && IsCopyCodec(state.OutputVideoCodec) |
| 0 | 6899 | | && _mediaEncoder.EncoderVersion >= _minFFmpegReadrateOption) |
| | 6900 | | { |
| | 6901 | | // Set an input read rate limit 10x for using SegmentDeletion with stream-copy |
| | 6902 | | // to prevent ffmpeg from exiting prematurely (due to fast drive) |
| 0 | 6903 | | inputModifier += " -readrate 10"; |
| | 6904 | | } |
| | 6905 | |
|
| 0 | 6906 | | var flags = new List<string>(); |
| 0 | 6907 | | if (state.IgnoreInputDts) |
| | 6908 | | { |
| 0 | 6909 | | flags.Add("+igndts"); |
| | 6910 | | } |
| | 6911 | |
|
| 0 | 6912 | | if (state.IgnoreInputIndex) |
| | 6913 | | { |
| 0 | 6914 | | flags.Add("+ignidx"); |
| | 6915 | | } |
| | 6916 | |
|
| 0 | 6917 | | if (state.GenPtsInput || IsCopyCodec(state.OutputVideoCodec)) |
| | 6918 | | { |
| 0 | 6919 | | flags.Add("+genpts"); |
| | 6920 | | } |
| | 6921 | |
|
| 0 | 6922 | | if (state.DiscardCorruptFramesInput) |
| | 6923 | | { |
| 0 | 6924 | | flags.Add("+discardcorrupt"); |
| | 6925 | | } |
| | 6926 | |
|
| 0 | 6927 | | if (state.EnableFastSeekInput) |
| | 6928 | | { |
| 0 | 6929 | | flags.Add("+fastseek"); |
| | 6930 | | } |
| | 6931 | |
|
| 0 | 6932 | | if (flags.Count > 0) |
| | 6933 | | { |
| 0 | 6934 | | inputModifier += " -fflags " + string.Join(string.Empty, flags); |
| | 6935 | | } |
| | 6936 | |
|
| 0 | 6937 | | if (state.IsVideoRequest) |
| | 6938 | | { |
| 0 | 6939 | | if (!string.IsNullOrEmpty(state.InputContainer) && state.VideoType == VideoType.VideoFile && encodingOpt |
| | 6940 | | { |
| 0 | 6941 | | var inputFormat = GetInputFormat(state.InputContainer); |
| 0 | 6942 | | if (!string.IsNullOrEmpty(inputFormat)) |
| | 6943 | | { |
| 0 | 6944 | | inputModifier += " -f " + inputFormat; |
| | 6945 | | } |
| | 6946 | | } |
| | 6947 | | } |
| | 6948 | |
|
| 0 | 6949 | | if (state.MediaSource.RequiresLooping) |
| | 6950 | | { |
| 0 | 6951 | | inputModifier += " -stream_loop -1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2"; |
| | 6952 | | } |
| | 6953 | |
|
| 0 | 6954 | | return inputModifier; |
| | 6955 | | } |
| | 6956 | |
|
| | 6957 | | public void AttachMediaSourceInfo( |
| | 6958 | | EncodingJobInfo state, |
| | 6959 | | EncodingOptions encodingOptions, |
| | 6960 | | MediaSourceInfo mediaSource, |
| | 6961 | | string requestedUrl) |
| | 6962 | | { |
| 0 | 6963 | | ArgumentNullException.ThrowIfNull(state); |
| | 6964 | |
|
| 0 | 6965 | | ArgumentNullException.ThrowIfNull(mediaSource); |
| | 6966 | |
|
| 0 | 6967 | | var path = mediaSource.Path; |
| 0 | 6968 | | var protocol = mediaSource.Protocol; |
| | 6969 | |
|
| 0 | 6970 | | if (!string.IsNullOrEmpty(mediaSource.EncoderPath) && mediaSource.EncoderProtocol.HasValue) |
| | 6971 | | { |
| 0 | 6972 | | path = mediaSource.EncoderPath; |
| 0 | 6973 | | protocol = mediaSource.EncoderProtocol.Value; |
| | 6974 | | } |
| | 6975 | |
|
| 0 | 6976 | | state.MediaPath = path; |
| 0 | 6977 | | state.InputProtocol = protocol; |
| 0 | 6978 | | state.InputContainer = mediaSource.Container; |
| 0 | 6979 | | state.RunTimeTicks = mediaSource.RunTimeTicks; |
| 0 | 6980 | | state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; |
| | 6981 | |
|
| 0 | 6982 | | state.IsoType = mediaSource.IsoType; |
| | 6983 | |
|
| 0 | 6984 | | if (mediaSource.Timestamp.HasValue) |
| | 6985 | | { |
| 0 | 6986 | | state.InputTimestamp = mediaSource.Timestamp.Value; |
| | 6987 | | } |
| | 6988 | |
|
| 0 | 6989 | | state.RunTimeTicks = mediaSource.RunTimeTicks; |
| 0 | 6990 | | state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; |
| 0 | 6991 | | state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate; |
| | 6992 | |
|
| 0 | 6993 | | if (state.ReadInputAtNativeFramerate |
| 0 | 6994 | | || (mediaSource.Protocol == MediaProtocol.File |
| 0 | 6995 | | && string.Equals(mediaSource.Container, "wtv", StringComparison.OrdinalIgnoreCase))) |
| | 6996 | | { |
| 0 | 6997 | | state.InputVideoSync = "-1"; |
| 0 | 6998 | | state.InputAudioSync = "1"; |
| | 6999 | | } |
| | 7000 | |
|
| 0 | 7001 | | if (string.Equals(mediaSource.Container, "wma", StringComparison.OrdinalIgnoreCase) |
| 0 | 7002 | | || string.Equals(mediaSource.Container, "asf", StringComparison.OrdinalIgnoreCase)) |
| | 7003 | | { |
| | 7004 | | // Seeing some stuttering when transcoding wma to audio-only HLS |
| 0 | 7005 | | state.InputAudioSync = "1"; |
| | 7006 | | } |
| | 7007 | |
|
| 0 | 7008 | | var mediaStreams = mediaSource.MediaStreams; |
| | 7009 | |
|
| 0 | 7010 | | if (state.IsVideoRequest) |
| | 7011 | | { |
| 0 | 7012 | | var videoRequest = state.BaseRequest; |
| | 7013 | |
|
| 0 | 7014 | | if (string.IsNullOrEmpty(videoRequest.VideoCodec)) |
| | 7015 | | { |
| 0 | 7016 | | if (string.IsNullOrEmpty(requestedUrl)) |
| | 7017 | | { |
| 0 | 7018 | | requestedUrl = "test." + videoRequest.Container; |
| | 7019 | | } |
| | 7020 | |
|
| 0 | 7021 | | videoRequest.VideoCodec = InferVideoCodec(requestedUrl); |
| | 7022 | | } |
| | 7023 | |
|
| 0 | 7024 | | state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video); |
| 0 | 7025 | | state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Su |
| 0 | 7026 | | state.SubtitleDeliveryMethod = videoRequest.SubtitleMethod; |
| 0 | 7027 | | state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio); |
| | 7028 | |
|
| 0 | 7029 | | if (state.SubtitleStream is not null && !state.SubtitleStream.IsExternal) |
| | 7030 | | { |
| 0 | 7031 | | state.InternalSubtitleStreamOffset = mediaStreams.Where(i => i.Type == MediaStreamType.Subtitle && ! |
| | 7032 | | } |
| | 7033 | |
|
| 0 | 7034 | | EnforceResolutionLimit(state); |
| | 7035 | |
|
| 0 | 7036 | | NormalizeSubtitleEmbed(state); |
| | 7037 | | } |
| | 7038 | | else |
| | 7039 | | { |
| 0 | 7040 | | state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true); |
| | 7041 | | } |
| | 7042 | |
|
| 0 | 7043 | | state.MediaSource = mediaSource; |
| | 7044 | |
|
| 0 | 7045 | | var request = state.BaseRequest; |
| 0 | 7046 | | var supportedAudioCodecs = state.SupportedAudioCodecs; |
| 0 | 7047 | | if (request is not null && supportedAudioCodecs is not null && supportedAudioCodecs.Length > 0) |
| | 7048 | | { |
| 0 | 7049 | | var supportedAudioCodecsList = supportedAudioCodecs.ToList(); |
| | 7050 | |
|
| 0 | 7051 | | ShiftAudioCodecsIfNeeded(supportedAudioCodecsList, state.AudioStream); |
| | 7052 | |
|
| 0 | 7053 | | state.SupportedAudioCodecs = supportedAudioCodecsList.ToArray(); |
| | 7054 | |
|
| 0 | 7055 | | request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault(_mediaEncoder.CanEncodeToAudioCodec) |
| 0 | 7056 | | ?? state.SupportedAudioCodecs.FirstOrDefault(); |
| | 7057 | | } |
| | 7058 | |
|
| 0 | 7059 | | var supportedVideoCodecs = state.SupportedVideoCodecs; |
| 0 | 7060 | | if (request is not null && supportedVideoCodecs is not null && supportedVideoCodecs.Length > 0) |
| | 7061 | | { |
| 0 | 7062 | | var supportedVideoCodecsList = supportedVideoCodecs.ToList(); |
| | 7063 | |
|
| 0 | 7064 | | ShiftVideoCodecsIfNeeded(supportedVideoCodecsList, encodingOptions); |
| | 7065 | |
|
| 0 | 7066 | | state.SupportedVideoCodecs = supportedVideoCodecsList.ToArray(); |
| | 7067 | |
|
| 0 | 7068 | | request.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault(); |
| | 7069 | | } |
| 0 | 7070 | | } |
| | 7071 | |
|
| | 7072 | | private void ShiftAudioCodecsIfNeeded(List<string> audioCodecs, MediaStream audioStream) |
| | 7073 | | { |
| | 7074 | | // No need to shift if there is only one supported audio codec. |
| 0 | 7075 | | if (audioCodecs.Count < 2) |
| | 7076 | | { |
| 0 | 7077 | | return; |
| | 7078 | | } |
| | 7079 | |
|
| 0 | 7080 | | var inputChannels = audioStream is null ? 6 : audioStream.Channels ?? 6; |
| 0 | 7081 | | var shiftAudioCodecs = new List<string>(); |
| 0 | 7082 | | if (inputChannels >= 6) |
| | 7083 | | { |
| | 7084 | | // DTS and TrueHD are not supported by HLS |
| | 7085 | | // Keep them in the supported codecs list, but shift them to the end of the list so that if transcoding |
| 0 | 7086 | | shiftAudioCodecs.Add("dts"); |
| 0 | 7087 | | shiftAudioCodecs.Add("truehd"); |
| | 7088 | | } |
| | 7089 | | else |
| | 7090 | | { |
| | 7091 | | // Transcoding to 2ch ac3 or eac3 almost always causes a playback failure |
| | 7092 | | // Keep them in the supported codecs list, but shift them to the end of the list so that if transcoding |
| 0 | 7093 | | shiftAudioCodecs.Add("ac3"); |
| 0 | 7094 | | shiftAudioCodecs.Add("eac3"); |
| | 7095 | | } |
| | 7096 | |
|
| 0 | 7097 | | if (audioCodecs.All(i => shiftAudioCodecs.Contains(i, StringComparison.OrdinalIgnoreCase))) |
| | 7098 | | { |
| 0 | 7099 | | return; |
| | 7100 | | } |
| | 7101 | |
|
| 0 | 7102 | | while (shiftAudioCodecs.Contains(audioCodecs[0], StringComparison.OrdinalIgnoreCase)) |
| | 7103 | | { |
| 0 | 7104 | | var removed = audioCodecs[0]; |
| 0 | 7105 | | audioCodecs.RemoveAt(0); |
| 0 | 7106 | | audioCodecs.Add(removed); |
| | 7107 | | } |
| 0 | 7108 | | } |
| | 7109 | |
|
| | 7110 | | private void ShiftVideoCodecsIfNeeded(List<string> videoCodecs, EncodingOptions encodingOptions) |
| | 7111 | | { |
| | 7112 | | // No need to shift if there is only one supported video codec. |
| 0 | 7113 | | if (videoCodecs.Count < 2) |
| | 7114 | | { |
| 0 | 7115 | | return; |
| | 7116 | | } |
| | 7117 | |
|
| | 7118 | | // Shift codecs to the end of list if it's not allowed. |
| 0 | 7119 | | var shiftVideoCodecs = new List<string>(); |
| 0 | 7120 | | if (!encodingOptions.AllowHevcEncoding) |
| | 7121 | | { |
| 0 | 7122 | | shiftVideoCodecs.Add("hevc"); |
| 0 | 7123 | | shiftVideoCodecs.Add("h265"); |
| | 7124 | | } |
| | 7125 | |
|
| 0 | 7126 | | if (!encodingOptions.AllowAv1Encoding) |
| | 7127 | | { |
| 0 | 7128 | | shiftVideoCodecs.Add("av1"); |
| | 7129 | | } |
| | 7130 | |
|
| 0 | 7131 | | if (videoCodecs.All(i => shiftVideoCodecs.Contains(i, StringComparison.OrdinalIgnoreCase))) |
| | 7132 | | { |
| 0 | 7133 | | return; |
| | 7134 | | } |
| | 7135 | |
|
| 0 | 7136 | | while (shiftVideoCodecs.Contains(videoCodecs[0], StringComparison.OrdinalIgnoreCase)) |
| | 7137 | | { |
| 0 | 7138 | | var removed = videoCodecs[0]; |
| 0 | 7139 | | videoCodecs.RemoveAt(0); |
| 0 | 7140 | | videoCodecs.Add(removed); |
| | 7141 | | } |
| 0 | 7142 | | } |
| | 7143 | |
|
| | 7144 | | private void NormalizeSubtitleEmbed(EncodingJobInfo state) |
| | 7145 | | { |
| 0 | 7146 | | if (state.SubtitleStream is null || state.SubtitleDeliveryMethod != SubtitleDeliveryMethod.Embed) |
| | 7147 | | { |
| 0 | 7148 | | return; |
| | 7149 | | } |
| | 7150 | |
|
| | 7151 | | // This is tricky to remux in, after converting to dvdsub it's not positioned correctly |
| | 7152 | | // Therefore, let's just burn it in |
| 0 | 7153 | | if (string.Equals(state.SubtitleStream.Codec, "DVBSUB", StringComparison.OrdinalIgnoreCase)) |
| | 7154 | | { |
| 0 | 7155 | | state.SubtitleDeliveryMethod = SubtitleDeliveryMethod.Encode; |
| | 7156 | | } |
| 0 | 7157 | | } |
| | 7158 | |
|
| | 7159 | | public string GetSubtitleEmbedArguments(EncodingJobInfo state) |
| | 7160 | | { |
| 0 | 7161 | | if (state.SubtitleStream is null || state.SubtitleDeliveryMethod != SubtitleDeliveryMethod.Embed) |
| | 7162 | | { |
| 0 | 7163 | | return string.Empty; |
| | 7164 | | } |
| | 7165 | |
|
| 0 | 7166 | | var format = state.SupportedSubtitleCodecs.FirstOrDefault(); |
| | 7167 | | string codec; |
| | 7168 | |
|
| 0 | 7169 | | if (string.IsNullOrEmpty(format) || string.Equals(format, state.SubtitleStream.Codec, StringComparison.Ordin |
| | 7170 | | { |
| 0 | 7171 | | codec = "copy"; |
| | 7172 | | } |
| | 7173 | | else |
| | 7174 | | { |
| 0 | 7175 | | codec = format; |
| | 7176 | | } |
| | 7177 | |
|
| 0 | 7178 | | return " -codec:s:0 " + codec + " -disposition:s:0 default"; |
| | 7179 | | } |
| | 7180 | |
|
| | 7181 | | public string GetProgressiveVideoFullCommandLine(EncodingJobInfo state, EncodingOptions encodingOptions, Encoder |
| | 7182 | | { |
| | 7183 | | // Get the output codec name |
| 0 | 7184 | | var videoCodec = GetVideoEncoder(state, encodingOptions); |
| | 7185 | |
|
| 0 | 7186 | | var format = string.Empty; |
| 0 | 7187 | | var keyFrame = string.Empty; |
| 0 | 7188 | | var outputPath = state.OutputFilePath; |
| | 7189 | |
|
| 0 | 7190 | | if (Path.GetExtension(outputPath.AsSpan()).Equals(".mp4", StringComparison.OrdinalIgnoreCase) |
| 0 | 7191 | | && state.BaseRequest.Context == EncodingContext.Streaming) |
| | 7192 | | { |
| | 7193 | | // Comparison: https://github.com/jansmolders86/mediacenterjs/blob/master/lib/transcoding/desktop.js |
| 0 | 7194 | | format = " -f mp4 -movflags frag_keyframe+empty_moov+delay_moov"; |
| | 7195 | | } |
| | 7196 | |
|
| 0 | 7197 | | var threads = GetNumberOfThreads(state, encodingOptions, videoCodec); |
| | 7198 | |
|
| 0 | 7199 | | var inputModifier = GetInputModifier(state, encodingOptions, null); |
| | 7200 | |
|
| 0 | 7201 | | return string.Format( |
| 0 | 7202 | | CultureInfo.InvariantCulture, |
| 0 | 7203 | | "{0} {1}{2} {3} {4} -map_metadata -1 -map_chapters -1 -threads {5} {6}{7}{8} -y \"{9}\"", |
| 0 | 7204 | | inputModifier, |
| 0 | 7205 | | GetInputArgument(state, encodingOptions, null), |
| 0 | 7206 | | keyFrame, |
| 0 | 7207 | | GetMapArgs(state), |
| 0 | 7208 | | GetProgressiveVideoArguments(state, encodingOptions, videoCodec, defaultPreset), |
| 0 | 7209 | | threads, |
| 0 | 7210 | | GetProgressiveVideoAudioArguments(state, encodingOptions), |
| 0 | 7211 | | GetSubtitleEmbedArguments(state), |
| 0 | 7212 | | format, |
| 0 | 7213 | | outputPath).Trim(); |
| | 7214 | | } |
| | 7215 | |
|
| | 7216 | | public string GetOutputFFlags(EncodingJobInfo state) |
| | 7217 | | { |
| 0 | 7218 | | var flags = new List<string>(); |
| 0 | 7219 | | if (state.GenPtsOutput) |
| | 7220 | | { |
| 0 | 7221 | | flags.Add("+genpts"); |
| | 7222 | | } |
| | 7223 | |
|
| 0 | 7224 | | if (flags.Count > 0) |
| | 7225 | | { |
| 0 | 7226 | | return " -fflags " + string.Join(string.Empty, flags); |
| | 7227 | | } |
| | 7228 | |
|
| 0 | 7229 | | return string.Empty; |
| | 7230 | | } |
| | 7231 | |
|
| | 7232 | | public string GetProgressiveVideoArguments(EncodingJobInfo state, EncodingOptions encodingOptions, string videoC |
| | 7233 | | { |
| 0 | 7234 | | var args = "-codec:v:0 " + videoCodec; |
| | 7235 | |
|
| 0 | 7236 | | if (state.BaseRequest.EnableMpegtsM2TsMode) |
| | 7237 | | { |
| 0 | 7238 | | args += " -mpegts_m2ts_mode 1"; |
| | 7239 | | } |
| | 7240 | |
|
| 0 | 7241 | | if (IsCopyCodec(videoCodec)) |
| | 7242 | | { |
| 0 | 7243 | | if (state.VideoStream is not null |
| 0 | 7244 | | && string.Equals(state.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) |
| 0 | 7245 | | && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase)) |
| | 7246 | | { |
| 0 | 7247 | | string bitStreamArgs = GetBitStreamArgs(state.VideoStream); |
| 0 | 7248 | | if (!string.IsNullOrEmpty(bitStreamArgs)) |
| | 7249 | | { |
| 0 | 7250 | | args += " " + bitStreamArgs; |
| | 7251 | | } |
| | 7252 | | } |
| | 7253 | |
|
| 0 | 7254 | | if (state.RunTimeTicks.HasValue && state.BaseRequest.CopyTimestamps) |
| | 7255 | | { |
| 0 | 7256 | | args += " -copyts -avoid_negative_ts disabled -start_at_zero"; |
| | 7257 | | } |
| | 7258 | |
|
| 0 | 7259 | | if (!state.RunTimeTicks.HasValue) |
| | 7260 | | { |
| 0 | 7261 | | args += " -fflags +genpts"; |
| | 7262 | | } |
| | 7263 | | } |
| | 7264 | | else |
| | 7265 | | { |
| 0 | 7266 | | var keyFrameArg = string.Format( |
| 0 | 7267 | | CultureInfo.InvariantCulture, |
| 0 | 7268 | | " -force_key_frames \"expr:gte(t,n_forced*{0})\"", |
| 0 | 7269 | | 5); |
| | 7270 | |
|
| 0 | 7271 | | args += keyFrameArg; |
| | 7272 | |
|
| 0 | 7273 | | var hasGraphicalSubs = state.SubtitleStream is not null && !state.SubtitleStream.IsTextSubtitleStream && |
| | 7274 | |
|
| 0 | 7275 | | var hasCopyTs = false; |
| | 7276 | |
|
| | 7277 | | // video processing filters. |
| 0 | 7278 | | var videoProcessParam = GetVideoProcessingFilterParam(state, encodingOptions, videoCodec); |
| | 7279 | |
|
| 0 | 7280 | | var negativeMapArgs = GetNegativeMapArgsByFilters(state, videoProcessParam); |
| | 7281 | |
|
| 0 | 7282 | | args = negativeMapArgs + args + videoProcessParam; |
| | 7283 | |
|
| 0 | 7284 | | hasCopyTs = videoProcessParam.Contains("copyts", StringComparison.OrdinalIgnoreCase); |
| | 7285 | |
|
| 0 | 7286 | | if (state.RunTimeTicks.HasValue && state.BaseRequest.CopyTimestamps) |
| | 7287 | | { |
| 0 | 7288 | | if (!hasCopyTs) |
| | 7289 | | { |
| 0 | 7290 | | args += " -copyts"; |
| | 7291 | | } |
| | 7292 | |
|
| 0 | 7293 | | args += " -avoid_negative_ts disabled"; |
| | 7294 | |
|
| 0 | 7295 | | if (!(state.SubtitleStream is not null && state.SubtitleStream.IsExternal && !state.SubtitleStream.I |
| | 7296 | | { |
| 0 | 7297 | | args += " -start_at_zero"; |
| | 7298 | | } |
| | 7299 | | } |
| | 7300 | |
|
| 0 | 7301 | | var qualityParam = GetVideoQualityParam(state, videoCodec, encodingOptions, defaultPreset); |
| | 7302 | |
|
| 0 | 7303 | | if (!string.IsNullOrEmpty(qualityParam)) |
| | 7304 | | { |
| 0 | 7305 | | args += " " + qualityParam.Trim(); |
| | 7306 | | } |
| | 7307 | | } |
| | 7308 | |
|
| 0 | 7309 | | if (!string.IsNullOrEmpty(state.OutputVideoSync)) |
| | 7310 | | { |
| 0 | 7311 | | args += GetVideoSyncOption(state.OutputVideoSync, _mediaEncoder.EncoderVersion); |
| | 7312 | | } |
| | 7313 | |
|
| 0 | 7314 | | args += GetOutputFFlags(state); |
| | 7315 | |
|
| 0 | 7316 | | return args; |
| | 7317 | | } |
| | 7318 | |
|
| | 7319 | | public string GetProgressiveVideoAudioArguments(EncodingJobInfo state, EncodingOptions encodingOptions) |
| | 7320 | | { |
| | 7321 | | // If the video doesn't have an audio stream, return a default. |
| 0 | 7322 | | if (state.AudioStream is null && state.VideoStream is not null) |
| | 7323 | | { |
| 0 | 7324 | | return string.Empty; |
| | 7325 | | } |
| | 7326 | |
|
| | 7327 | | // Get the output codec name |
| 0 | 7328 | | var codec = GetAudioEncoder(state); |
| | 7329 | |
|
| 0 | 7330 | | var args = "-codec:a:0 " + codec; |
| | 7331 | |
|
| 0 | 7332 | | if (IsCopyCodec(codec)) |
| | 7333 | | { |
| 0 | 7334 | | return args; |
| | 7335 | | } |
| | 7336 | |
|
| 0 | 7337 | | var channels = state.OutputAudioChannels; |
| | 7338 | |
|
| 0 | 7339 | | var useDownMixAlgorithm = state.AudioStream is not null |
| 0 | 7340 | | && DownMixAlgorithmsHelper.AlgorithmFilterStrings.ContainsKey((encodingOptions.Dow |
| | 7341 | |
|
| 0 | 7342 | | if (channels.HasValue && !useDownMixAlgorithm) |
| | 7343 | | { |
| 0 | 7344 | | args += " -ac " + channels.Value; |
| | 7345 | | } |
| | 7346 | |
|
| 0 | 7347 | | var bitrate = state.OutputAudioBitrate; |
| 0 | 7348 | | if (bitrate.HasValue && !LosslessAudioCodecs.Contains(codec, StringComparison.OrdinalIgnoreCase)) |
| | 7349 | | { |
| 0 | 7350 | | var vbrParam = GetAudioVbrModeParam(codec, bitrate.Value, channels ?? 2); |
| 0 | 7351 | | if (encodingOptions.EnableAudioVbr && state.EnableAudioVbrEncoding && vbrParam is not null) |
| | 7352 | | { |
| 0 | 7353 | | args += vbrParam; |
| | 7354 | | } |
| | 7355 | | else |
| | 7356 | | { |
| 0 | 7357 | | args += " -ab " + bitrate.Value.ToString(CultureInfo.InvariantCulture); |
| | 7358 | | } |
| | 7359 | | } |
| | 7360 | |
|
| 0 | 7361 | | if (state.OutputAudioSampleRate.HasValue) |
| | 7362 | | { |
| 0 | 7363 | | args += " -ar " + state.OutputAudioSampleRate.Value.ToString(CultureInfo.InvariantCulture); |
| | 7364 | | } |
| | 7365 | |
|
| 0 | 7366 | | args += GetAudioFilterParam(state, encodingOptions); |
| | 7367 | |
|
| 0 | 7368 | | return args; |
| | 7369 | | } |
| | 7370 | |
|
| | 7371 | | public string GetProgressiveAudioFullCommandLine(EncodingJobInfo state, EncodingOptions encodingOptions, string |
| | 7372 | | { |
| 0 | 7373 | | var audioTranscodeParams = new List<string>(); |
| | 7374 | |
|
| 0 | 7375 | | var bitrate = state.OutputAudioBitrate; |
| 0 | 7376 | | var channels = state.OutputAudioChannels; |
| 0 | 7377 | | var outputCodec = state.OutputAudioCodec; |
| | 7378 | |
|
| 0 | 7379 | | if (bitrate.HasValue && !LosslessAudioCodecs.Contains(outputCodec, StringComparison.OrdinalIgnoreCase)) |
| | 7380 | | { |
| 0 | 7381 | | var vbrParam = GetAudioVbrModeParam(GetAudioEncoder(state), bitrate.Value, channels ?? 2); |
| 0 | 7382 | | if (encodingOptions.EnableAudioVbr && state.EnableAudioVbrEncoding && vbrParam is not null) |
| | 7383 | | { |
| 0 | 7384 | | audioTranscodeParams.Add(vbrParam); |
| | 7385 | | } |
| | 7386 | | else |
| | 7387 | | { |
| 0 | 7388 | | audioTranscodeParams.Add("-ab " + bitrate.Value.ToString(CultureInfo.InvariantCulture)); |
| | 7389 | | } |
| | 7390 | | } |
| | 7391 | |
|
| 0 | 7392 | | if (channels.HasValue) |
| | 7393 | | { |
| 0 | 7394 | | audioTranscodeParams.Add("-ac " + state.OutputAudioChannels.Value.ToString(CultureInfo.InvariantCulture) |
| | 7395 | | } |
| | 7396 | |
|
| 0 | 7397 | | if (!string.IsNullOrEmpty(outputCodec)) |
| | 7398 | | { |
| 0 | 7399 | | audioTranscodeParams.Add("-acodec " + GetAudioEncoder(state)); |
| | 7400 | | } |
| | 7401 | |
|
| 0 | 7402 | | if (GetAudioEncoder(state).StartsWith("pcm_", StringComparison.Ordinal)) |
| | 7403 | | { |
| 0 | 7404 | | audioTranscodeParams.Add(string.Concat("-f ", GetAudioEncoder(state).AsSpan(4))); |
| 0 | 7405 | | audioTranscodeParams.Add("-ar " + state.BaseRequest.AudioBitRate); |
| | 7406 | | } |
| | 7407 | |
|
| 0 | 7408 | | if (!string.Equals(outputCodec, "opus", StringComparison.OrdinalIgnoreCase)) |
| | 7409 | | { |
| | 7410 | | // opus only supports specific sampling rates |
| 0 | 7411 | | var sampleRate = state.OutputAudioSampleRate; |
| 0 | 7412 | | if (sampleRate.HasValue) |
| | 7413 | | { |
| 0 | 7414 | | var sampleRateValue = sampleRate.Value switch |
| 0 | 7415 | | { |
| 0 | 7416 | | <= 8000 => 8000, |
| 0 | 7417 | | <= 12000 => 12000, |
| 0 | 7418 | | <= 16000 => 16000, |
| 0 | 7419 | | <= 24000 => 24000, |
| 0 | 7420 | | _ => 48000 |
| 0 | 7421 | | }; |
| | 7422 | |
|
| 0 | 7423 | | audioTranscodeParams.Add("-ar " + sampleRateValue.ToString(CultureInfo.InvariantCulture)); |
| | 7424 | | } |
| | 7425 | | } |
| | 7426 | |
|
| | 7427 | | // Copy the movflags from GetProgressiveVideoFullCommandLine |
| | 7428 | | // See #9248 and the associated PR for why this is needed |
| 0 | 7429 | | if (_mp4ContainerNames.Contains(state.OutputContainer)) |
| | 7430 | | { |
| 0 | 7431 | | audioTranscodeParams.Add("-movflags empty_moov+delay_moov"); |
| | 7432 | | } |
| | 7433 | |
|
| 0 | 7434 | | var threads = GetNumberOfThreads(state, encodingOptions, null); |
| | 7435 | |
|
| 0 | 7436 | | var inputModifier = GetInputModifier(state, encodingOptions, null); |
| | 7437 | |
|
| 0 | 7438 | | return string.Format( |
| 0 | 7439 | | CultureInfo.InvariantCulture, |
| 0 | 7440 | | "{0} {1}{7}{8} -threads {2}{3} {4} -id3v2_version 3 -write_id3v1 1{6} -y \"{5}\"", |
| 0 | 7441 | | inputModifier, |
| 0 | 7442 | | GetInputArgument(state, encodingOptions, null), |
| 0 | 7443 | | threads, |
| 0 | 7444 | | " -vn", |
| 0 | 7445 | | string.Join(' ', audioTranscodeParams), |
| 0 | 7446 | | outputPath, |
| 0 | 7447 | | string.Empty, |
| 0 | 7448 | | string.Empty, |
| 0 | 7449 | | string.Empty).Trim(); |
| | 7450 | | } |
| | 7451 | |
|
| | 7452 | | public static int FindIndex(IReadOnlyList<MediaStream> mediaStreams, MediaStream streamToFind) |
| | 7453 | | { |
| 0 | 7454 | | var index = 0; |
| 0 | 7455 | | var length = mediaStreams.Count; |
| | 7456 | |
|
| 0 | 7457 | | for (var i = 0; i < length; i++) |
| | 7458 | | { |
| 0 | 7459 | | var currentMediaStream = mediaStreams[i]; |
| 0 | 7460 | | if (currentMediaStream == streamToFind) |
| | 7461 | | { |
| 0 | 7462 | | return index; |
| | 7463 | | } |
| | 7464 | |
|
| 0 | 7465 | | if (string.Equals(currentMediaStream.Path, streamToFind.Path, StringComparison.Ordinal)) |
| | 7466 | | { |
| 0 | 7467 | | index++; |
| | 7468 | | } |
| | 7469 | | } |
| | 7470 | |
|
| 0 | 7471 | | return -1; |
| | 7472 | | } |
| | 7473 | |
|
| | 7474 | | public static bool IsCopyCodec(string codec) |
| | 7475 | | { |
| 0 | 7476 | | return string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase); |
| | 7477 | | } |
| | 7478 | |
|
| | 7479 | | private static bool ShouldEncodeSubtitle(EncodingJobInfo state) |
| | 7480 | | { |
| 0 | 7481 | | return state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode |
| 0 | 7482 | | || (state.BaseRequest.AlwaysBurnInSubtitleWhenTranscoding && !IsCopyCodec(state.OutputVideoCodec)); |
| | 7483 | | } |
| | 7484 | |
|
| | 7485 | | public static string GetVideoSyncOption(string videoSync, Version encoderVersion) |
| | 7486 | | { |
| 0 | 7487 | | if (string.IsNullOrEmpty(videoSync)) |
| | 7488 | | { |
| 0 | 7489 | | return string.Empty; |
| | 7490 | | } |
| | 7491 | |
|
| 0 | 7492 | | if (encoderVersion >= new Version(5, 1)) |
| | 7493 | | { |
| 0 | 7494 | | if (int.TryParse(videoSync, CultureInfo.InvariantCulture, out var vsync)) |
| | 7495 | | { |
| 0 | 7496 | | return vsync switch |
| 0 | 7497 | | { |
| 0 | 7498 | | -1 => " -fps_mode auto", |
| 0 | 7499 | | 0 => " -fps_mode passthrough", |
| 0 | 7500 | | 1 => " -fps_mode cfr", |
| 0 | 7501 | | 2 => " -fps_mode vfr", |
| 0 | 7502 | | _ => string.Empty |
| 0 | 7503 | | }; |
| | 7504 | | } |
| | 7505 | |
|
| 0 | 7506 | | return string.Empty; |
| | 7507 | | } |
| | 7508 | |
|
| | 7509 | | // -vsync is deprecated in FFmpeg 5.1 and will be removed in the future. |
| 0 | 7510 | | return $" -vsync {videoSync}"; |
| | 7511 | | } |
| | 7512 | | } |
| | 7513 | | } |